日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python之matloplib可视化

發布時間:2025/4/16 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python之matloplib可视化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import matplotlib.pyplot as plt import numpy as npx = np.linspace(-10, 20, 50, True) y = 2 * x + 5# 一、畫散點圖 plt.scatter(x,y,s=40,c='Orange',edgecolors='k',marker='s',alpha=0.5) #s:大小;c:內部顏色;edgecolors:邊框顏色;marker:點的形狀;alpha:透明度 plt.show()# 二、畫折線圖與線段 plt.figure() plt.plot(x, y, label='line1') plt.plot(x, y + 4, c='r', lw=2, ls=':', label='line2') # c:顏色;lw:線寬;ls:線段類型,有“:”“--”“-”等;lebel:畫圖圖例; plt.plot([-10, 0], [-10, 0], ls='--', label='line3') # 兩點連線 plt.legend() plt.show()# 三、畫柱狀圖 # 1、單一柱狀圖 plt.figure() bar_x = np.arange(5) bar_y = np.arange(5) rects = plt.bar(bar_x, height=bar_y, width=0.8,orientation='vertical') # 給每個柱添加數據標識 for rect in rects:plt.text(rect.get_x() + rect.get_width() / 2, rect.get_height(), rect.get_height(), ha='center', va='bottom') plt.show()# 2、橫向柱狀圖 # method 1 : plt.figure() plt.barh(bar_y,bar_x,height=0.8,left=0) #left柱狀圖左邊的起始位置,0表示從x=0開始 plt.show()# method 2 : plt.figure() plt.bar(x=0,bottom=bar_x, width=bar_y, height=0.5,orientation='horizontal') #bottom:柱狀圖底部起始位置為y=bar_x,orientation設置柱狀圖方向 plt.show()# 3、并列圖 plt.figure() plt.bar(bar_x, height=bar_y, width=0.2,orientation='vertical',color='b') #第一個藍色柱狀圖 plt.bar(bar_x+0.2, height=bar_y, width=0.2,orientation='vertical',color='r') #第二個紅色柱狀圖,用bar_x+0.2設置寬度及柱狀圖偏移的方式將其與第一個圖分開 plt.show()# 4、堆疊圖 plt.figure() plt.bar(bar_x, height=bar_y, width=0.2,orientation='vertical',color='b') #第一個藍色柱狀圖 plt.bar(bar_x, height=bar_y, width=0.2,bottom=bar_x,orientation='vertical',color='r') #第二個紅色柱狀圖,用bottom=bar_x設置底部起點的方式將其與第一個圖分開 plt.show()# 四、直方圖(頻數圖) plt.hist(np.random.normal(0,2,1000),bins=100,density=True) #bins:分箱個數,若傳入一個list,如[0,1,2,3]表示根據0、1、2、3位斷點分箱 plt.show()# 五、餅狀圖 plt.figure() plt.axes(aspect=1) #設置xy比例不然餅狀圖會被壓扁 plt.pie([1,2,3,4,5],explode=[0.5,0,0,0,0],labels=list('abcde'),autopct='%.2f%%',shadow=True) #explode:一個list表示每一部分離中心的距離,autopct='%.2f%%'顯示百分比,shadow=True生成陰影更好看 plt.show()# 六、箱形圖 plt.figure() plt.boxplot(np.random.normal(0,1,1000),sym='o',whis=1.5) #sym異常點顯示類型,whis設置異常點范圍,[Q1-whis*IQR,Q3+whis*IQR]以外的為異常值 plt.figure() plt.boxplot(np.random.normal(0,1,(1000,4)),sym='o',whis=1.5) #傳入多維數組可以畫多個箱形圖 plt.show()# 七、畫坐標軸為日期的圖 d=['2019-01','2019-02','2019-03','2019-04','2019-05','2019-06','2019-07','2019-08','2019-09','2019-10','2019-11','2019-12'] plt.plot_date(d,y[0:12]) plt.show()# 八、設置畫板figure plt.figure(num='fig1',figsize=(8,6),facecolor='w',edgecolor='k') #num:畫板名,figsize=(8,6)畫板長寬,facecolor:底色,edgecolor邊框顏色 plt.title('fig1') 添加圖標題 plt.plot([0,1],[0,1]) plt.figure(num='fig2') #定第二個畫板,可以顯示兩個圖,不然會在原來的fig2上覆蓋 plt.title('fig2') plt.plot([0,1],[1,0]) # 獲取當前圖標與子圖可以使用plt.gcf()和plt.gca()獲得,分別表示Get Current Figure和Get Current Axes,在pyplot模塊中, # 許多函數都是對當前的Figure或Axes對象進行處理,比如說:plt.plot()實際上會通過plt.gca()獲得當前的Axes對象ax,然后再調用ax.plot()方法實現真正的繪圖。 plt.show()# 九、坐標軸設置 plt.figure() plt.title('fig1') plt.xlim((0, 10)) # 修改x坐標軸范圍 plt.plot([0, 20], [20, 0]) plt.xlabel(r'$x\ label$') # 添加x軸標簽,matplotlib中使用$$表示latex數學字符,空格要用\轉義 plt.figure() plt.title('fig2') plt.xticks(np.linspace(0, 10, 3, True)) # 修改x標簽的數值間隔 plt.plot([0, 10], [10, 0])plt.figure() plt.title('fig3') plt.xticks(ticks=np.linspace(0, 10, 3, True), labels=['2015', '2016', '2017']) # 用lebals替換x軸ticks的對應數值 plt.plot([0, 10], [10, 0])plt.figure() plt.title('fig4') ax = plt.gca() ax.spines['right'].set_color('None') # 去除右坐標軸顏色,相當于隱藏,spines表示子圖方框線集合,left、bottom、right、top表示四個方框線 ax.spines['top'].set_color('None') # 去除上坐標軸顏色,相當于隱藏 ax.xaxis.set_ticks_position('bottom') # 設置x軸位置為底部,可設置為上側top ax.yaxis.set_ticks_position('left') # 設置y軸位置為左側,可設置為右側right ax.spines['bottom'].set_position(('data', 5)) # 底部的坐標軸放在y=5的位置,data表示采用數據軸,(data,5)替換成(axes,0.5)則表示放在y軸%50的位置 ax.spines['left'].set_position(('data', 5)) # 左側的坐標軸放在x=5的位置 plt.plot([0, 10], [10, 0]) plt.show()#十、繪制圖例 plt.figure() l1,=plt.plot(x,y,c='g',label='y=2*x+5') #返回值不止一個所以l1后面加一個逗號,參數label為畫圖圖例 l2,=plt.plot(x,-y,c='y',label='y=2*x+5') plt.legend(loc='upper center') #顯示所有畫圖圖例,參數loc為圖例位置,默認為loc='best',自動選取位置 plt.figure() l1,=plt.plot(x,y,c='g',label='y=2*x+5') l2,=plt.plot(x,-y,c='y',label='y=2*x+5') plt.legend(handles=[l1,l2],labels=['f=2*x+5','f=2*x+5'],loc='') #對特定的圖添加指定的圖例說明,參數handles為line對象 plt.show()# 十一、添加注解 # 1、給點添加注解 plt.figure() plt.scatter(1, 1, c='r', marker='o') # 畫(1,1)點 plt.annotate(s=r'$this\ is\ an\ annotate$', xy=(1, 1), xycoords='data', xytext=(30, -30), textcoords='offset points',fontsize=16, arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2')) #s:注釋;xy:要注釋的點的坐標;xycoords='data':點的坐標為數據坐標;xytext=(30, -30):注釋的坐標為(30,-30); # textcoords='offset points':注釋的坐標軸為以點為原點進行偏移;fontsize:字體大小; # arrowprops:設定箭頭,arrowstyle箭頭類型為'->',connectionstyle連接類型為'arc3,rad=.2'# 2、在指定位置添加注解 plt.figure() plt.scatter(1, 1, c='r', marker='o') # 畫(1,1)點 plt.text(1,1,r'$this\ is\ a\ text$',fontdict=dict(size=16,color='r'), ha='center', va='bottom') #在點(1,1)處添加文檔,ha、va表示左右上下對齊方式 plt.show()# 十二、繪制多個圖在一個面板中 # method 1 :在大小相同的格子中畫圖 plt.subplot(221) #畫板分為2*2,在位置1畫圖 plt.plot(x, y) plt.scatter(5,0,marker='o',edgecolors='b',facecolors='r') plt.subplot(222) plt.plot(x,-y) plt.show()# method 2 :在自定義大小的格子中畫圖 ax1=plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1) #畫板分成3*3網格,ax1位置在(0,0),跨度為3列1行 ax1.plot([0,1],[0,1]) ax1.set_title('ax1') ax2=plt.subplot2grid((3,3),(1,1),colspan=3,rowspan=3) #畫板分成3*3網格,ax1位置在(1,1),跨度為3列3行 ax2.plot(x,-y) ax2.set_title('ax2') ax3=plt.subplot2grid((3,3),(1,0),colspan=1,rowspan=1) #畫板分成3*3網格,ax1位置在(1,0),跨度為1列1行 ax3.plot(x,-y) ax3.set_title('ax3') plt.show()# 十三、圖中圖,利用在同一畫板定義多個坐標軸實現 fig=plt.figure() left,bottom,width,height=0.1,0.1,0.8,0.8 #定義坐標軸的左、下坐標軸位置,寬、高的跨度,用百分比的形式 ax1=fig.add_axes([left,bottom,width,height]) #加入第一個坐標軸 ax1.plot(x,y,c='y') ax1.set_titile('ax1') ax2=fig.add_axes([left,bottom,width,height]) #加入第二個坐標軸 ax2.plot(x,-y,c='g') ax2.set_titile('ax2') plt.show()# 十四、主次坐標軸 fig,ax1=plt.subplots() ax2=ax1.twinx() #鏡像ax1得到次坐標軸 ax1.plot([0,1],[0,1],c='r') ax2.plot([0,1],[1,0],c='y') plt.show()

?

轉載于:https://www.cnblogs.com/dwithy/p/11195374.html

總結

以上是生活随笔為你收集整理的python之matloplib可视化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。