Matplotlib画图常用方法总结(全)
Matplotlib畫圖指南
常見的python的matplotlib 的畫圖的方法,畫散點圖,曲線圖,子圖,以及子圖的布局,間距等。
?
1:首先導入包,因為畫圖常用的數據類型我們需要導入包numpy
import numpy as np
import matplotlib.pyplot as plt
2:創建新圖
fig=plt.figure()
????????? Fig=plt.figure(1)
? ?????使用add_subplot創建多個子圖 subplot
??? ???Ax1=fig.add_subpot(2,2,1)
新圖和子圖之間的關系:
3:使用plot()函數畫圖
plot()畫曲線圖,可設置點的格式,線的格式。
Scatter()劃散點圖,可以設置散點的樣式。
matplotlib.pyplot.scatter(x, y,s=20, c=None, marker='o',?cmap=None, norm=None, vmin=None, vmax=None,alpha=None,?linewidths=None, verts=None, edgecolors=None,hold=None,?data=None, **kwargs)?
基本的x ,y參數,還有c,s,alpha和marker,c就是為點指定的顏色數組,s是點的面積大小,alpha是點的顏色的透明度,marker是指定點標記的形狀,linewidths為線寬。在例子里指定透明度為0.5,c表示顏色常見的顏色為?['r','y','g','b','r','y','g','b','r']?,
S[1,2,3,4,5…100]表示形狀的面積。我們要改變的是marker的值,marker有很多值可供選擇,下表展示了在例子代碼的基礎上,改變marker的值后的效果:
·????????使用HTML十六進制字符串?color='eeefff'?使用合法的HTML顏色名字(’red’,’chartreuse’等)。
·????????也可以傳入一個歸一化到[0,1]的RGB元祖。?color=(0.3,0.3,0.4)
用來該表線條的屬性
線條風格linestyle或ls | 描述 | 線條風格linestyle或ls | 描述 | |
‘-‘ | 實線 | ‘:’ | 虛線 | ? |
‘–’ | 破折線 | ‘None’,’ ‘,’’ | 什么都不畫 | ? |
‘-.’ |
聯合使用‘顏色標記線條屬性’'bs-.','go-'
?
??
plt.plot(t,t, 'r--', t, t**2, 'bs', t, t**3, 'g^'),疊加圖
plot(X,S, color="red", linewidth=2.5, linestyle="-", label="sine")
plot(X,C, color="blue", linewidth=2.5, linestyle='-', label='cosine')
簡寫版:
plt.plot([1,2,3],[1,2,3], 'go-', label='line 1', linewidth=2)
plt.scatter(x,y, s=area, c=colors, alpha=0.5, marker=(9, 3, 30))
也可以不斷地修改顏色和值的大小和形狀樣式
?
5:細化橫軸和縱軸的設置,設置橫軸、縱軸的界限以及標注
plt.xlim(X.min(),X.max())
plt.ylim(y.min(),y.max())
或者
plt.axis([X.min(),X.max(),y.min(), y.max()])
6:設置軸記號
plt.xticks()/plt.yticks()設置軸記號
例如:xticks([-np.pi, -np.pi/2, 0, np.pi/2,np.pi],
[r'$-\pi$',r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
yticks([-1,0, +1],[r'$-1$', r'$0$', r'$+1$'])
?
有plt.xticks([1,2,3,…])會根據設置來顯示標記
沒有plt.xticks([1,2,3,…])會根據具體情況來自動設置標記(比較好,省事)。
7:plt.legend()添加圖例
Legend(loc):使用loc參數
plt.legend(loc='lower left')
0: ‘best' 1: ‘upper right' 2: ‘upper left' 3: ‘lower left' | 4: ‘lower right' 5: ‘right' 6: ‘center left' | 7: ‘center right' 8: ‘lower center' 9: ‘upper center' 10: ‘center' |
plt.legend(loc='best',frameon=False) #去掉圖例邊框,推薦使用,效果比較美觀。
plt.legend(loc='best',edgecolor='blue') #設置圖例邊框顏色
plt.legend(loc='best',facecolor='blue') #設置圖例背景顏色,若無邊框,參數無效
?
用法1:
plot(X, C, color="blue", linewidth=2.5,linestyle="-", label="cosine")
plot(X, S, color="red", linewidth=2.5,linestyle="-", label="sine")
legend(loc='upper left')
用法2:plt.plot(x, y, color="red",linewidth=2.5, linestyle="-")
??????? Plt.legend('圖例名',loc='best',frameon=False)
用法3;顯示多個圖例
p1, = plt.plot([1,2,3])
p2, = plt.plot([3,2,1])
l1 = plt.legend([p2, p1], ["line 2", "line1"], loc='upper left')
8:添加文字說明
·????????text()可以在圖中的任意位置添加文字,并支持LaTex語法
·????????xlable(), ylable()用于添加x軸和y軸標簽
·????????title()用于添加圖的題目
? plt.xlabel('Smarts')??? plt.ylabel('Probability')
#添加標題? plt.title('Histogram of IQ')
#添加文字? plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
9:讓圖片能夠正常顯示負數和中文:
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
mpl.rcParams['axes.unicode_minus']=False#用來正常顯示負號
?
plt.plot(a,'b.',markersize=10, label=u'真'),
中文使用之前加u 如data[u’身高’]
附加的設置:
1:plt.figure(figsize=(12,16)),設置圖片的大小
2:調整子圖的距離。
plt.tight_layout()#調整每隔子圖之間的距離。自動調整,有時候效果也不太好。
調整子圖之間的邊距;
plt.subplots_adjust(left=None, bottom=None, right=None, top=None,
??????????????? ???????wspace=None, hspace=None)
left? = 0.125 ?# the left side of thesubplots of the figure
right = 0.9?? ?# the right side of the subplots of the figure
bottom = 0.1? ?# the bottom of the subplots of the figure
top = 0.9???? ?# the top of the subplots of the figure
wspace = 0.2? ?# the amount of width reserved for blank space between subplots,
????????????# expressed as a fraction of the average axis width
hspace = 0.2? ?# the amount of height reserved for white space between subplots,
????????????? ?# expressed as a fraction of the average axis height
3:子圖的布局
首先按照(2,2)劃分,第一個圖為(2,2,1)和(2,2,2)
然后把下面兩個圖合成一個,則上面兩個圖為(2,1,2)
?
最后:舉一個栗子
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標簽
mpl.rcParams['axes.unicode_minus']=False #用來正常顯示負號
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,10)
y = x
fig = plt.figure(1)
ax1 = fig.add_subplot(211)
ax1.set_title('圖一的第一張子圖')
plt.xlabel('X1')
plt.ylabel('Y1')
sValue = x*10
ax1.scatter(x,y,s=sValue,c='r',marker='1')
plt.axis([0, 10,0,10])
ax1.legend('圖例1.1')
ax2 = fig.add_subplot(212)
ax2.set_title('圖一的第二張子圖')
plt.xlabel('X12')
plt.ylabel('Y12')
plt.plot(x, y, 'go-')
plt.axis([0, 10,0,10])
ax2.legend('圖例1.2')
x = np.arange(1,100)
y = x
fig = plt.figure(2)
ax1 = fig.add_subplot(211)
ax1.set_title('圖2的第一張子圖')
plt.xlabel('X1')
plt.ylabel('Y1')
sValue = x*10
colo=['r','y','g','b','r','y','g','b','r','m']
ax1.scatter(x,y,s=sValue ,c=colo)
plt.axis([0, 10,0,10])
ax1.legend('圖例1.1')
ax2 = fig.add_subplot(212)
#設置標題
ax2.set_title('圖二的第二張子圖')
#設置X軸標簽
plt.xlabel('X12')
#設置Y軸標簽
plt.ylabel('Y12')
#畫折線圖
plt.plot(x, y, color="red", linewidth=2.5, linestyle="-")
plt.axis([0, 100,0,100])
#設置圖標
ax2.legend('圖例1.2',loc='best',frameon=False)
plt.show()
?
?
?
? ?
?
總結
以上是生活随笔為你收集整理的Matplotlib画图常用方法总结(全)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv之图象裁剪
- 下一篇: 如何将两个虚拟机ping通?如何让虚拟机