python matplotlib 绘图
生活随笔
收集整理的這篇文章主要介紹了
python matplotlib 绘图
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.散點(diǎn)圖
import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei']#正常顯示字體 mpl.rcParams['axes.unicode_minus'] = False#正常顯示圖像中的負(fù)號(hào)x=np.random.randint(low=2,high=10,size=10) y=np.random.randint(low=2,high=10,size=10) plt.scatter(x,y) plt.title('散點(diǎn)圖') plt.xlabel('x') plt.ylabel('y') plt.show()2.折線圖
import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsex=np.linspace(start=0,stop=30,num=300) y=np.sin(x) plt.plot(x,y) plt.title('折線圖') plt.xlabel('x') plt.ylabel('y') plt.show()3.柱狀圖
更多柱狀圖
python畫(huà)柱狀圖并數(shù)值顯示
4.直方圖
import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsex=np.random.normal(loc=10,scale=1,size=100)plt.hist(x,bins=50) plt.title('直方圖') plt.xlabel('x') plt.ylabel('y') plt.show()6.圖形屬性
常用的顏色屬性
marker可選參數(shù)
'.' point marker ',' pixel marker 'o' circle marker 'v' triangle_down marker '^' triangle_up marker '<' triangle_left marker '>' triangle_right marker '1' tri_down marker '2' tri_up marker '3' tri_left marker '4' tri_right marker 's' square marker 'p' pentagon marker '*' star marker 'h' hexagon1 marker 'H' hexagon2 marker '+' plus marker 'x' x marker 'D' diamond marker 'd' thin_diamond marker '|' vline marker '_' hline markerlinestyle可選參數(shù):
'-' solid line style '--' dashed line style '-.' dash-dot line style ':' dotted line style import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsex=np.linspace(start=0,stop=30,num=30) y=np.sin(x) plt.plot(x,y,color='r',marker='d',linestyle='--',linewidth=2,alpha=0.8) plt.title("顏色:紅,標(biāo)記:菱形,線性:虛線,線寬:2,透明度:0.8") plt.xlabel('x') plt.ylabel('y') plt.show()7.子圖
import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 ax1=fig.add_subplot(2,2,1)#添加一個(gè)子圖,返回子圖句柄 ax2=fig.add_subplot(2,2,2) ax3=fig.add_subplot(2,2,3) ax4=fig.add_subplot(2,2,4)#子圖1 x=np.linspace(start=0,stop=30,num=30) y=sin(x) ax1.plot(x,y) ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('子圖1')#子圖2 x=np.random.randint(low=2,high=10,size=10) y=np.random.randint(low=2,high=10,size=10) ax2.scatter(x,y) ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_title('子圖2') #子圖3 x=['a','b','c','d'] y=[3,5,7,9] ax3.bar(x,y,width=0.5) ax3.set_xlabel('x') ax3.set_ylabel('y') ax3.set_title('子圖3')#子圖4 x=np.random.normal(loc=10,scale=1,size=100) ax4.hist(x,bins=50) ax4.set_xlabel('x') ax4.set_ylabel('y') ax4.set_title('子圖4')plt.show()或者
import numpy as np import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 #fig.suptitle('大標(biāo)題', fontsize=18)#子圖1 plt.subplot(2,2,1) x=np.linspace(start=0,stop=30,num=30) y=sin(x) plt.plot(x,y) plt.xlabel('x') plt.ylabel('y') plt.title('子圖1')#子圖2 plt.subplot(2,2,2) x=np.random.randint(low=2,high=10,size=10) y=np.random.randint(low=2,high=10,size=10) plt.scatter(x,y) plt.xlabel('x') plt.ylabel('y') plt.title('子圖2') #子圖3 plt.subplot(2,2,3) x=['a','b','c','d'] y=[3,5,7,9] plt.bar(x,y,width=0.5) plt.xlabel('x') plt.ylabel('y') plt.title('子圖3')#子圖4 plt.subplot(2,2,4) x=np.random.normal(loc=10,scale=1,size=100) plt.hist(x,bins=50) plt.xlabel('x') plt.ylabel('y') plt.title('子圖4') plt.show()8.三維圖:曲線
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 ax=fig.gca(projection='3d')#指定為3D#子圖1 theta=np.linspace(-4*np.pi,4*np.pi,100)z=np.linspace(start=-2,stop=2,num=100) r=z**2+1 x=r*np.sin(theta) y=z*np.cos(theta) ax.plot(x,y,z) plt.show()9.三維圖:散點(diǎn)圖
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 ax=fig.gca(projection='3d')#指定為3D#子圖1 x1=np.random.random(100)*20 y1=np.random.random(100)*20 z=x1+y1 ax.scatter(x1,y1,z) plt.show()10.三維圖:曲面圖
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 ax=fig.gca(projection='3d')#指定為3Dx=np.arange(-5,5,0.25) y=np.arange(-5,5,0.25) x,y=np.meshgrid(x,y)#用np.meshgrid生成坐標(biāo)網(wǎng)格矩陣 z=np.sin(np.sqrt(x**2+y**2)) #使用plot_surface函數(shù) #cmap=cm.coolwarm是顏色屬性 surf=ax.plot_surface(x,y,z,cmap=cm.coolwarm) plt.show()11.動(dòng)態(tài)圖
import matplotlib.pyplot as plt import numpy as npPOINTS = 100 sin_list = [0] * POINTS indx = 0 # fig, ax = plt.subplots() while True:if indx == 40:indx = 0indx += 1# 更新繪圖數(shù)據(jù)sin_list = sin_list[1:] + [np.sin((indx / 20) * np.pi)]# 顯示時(shí)間plt.pause(0.01)# 清除上一次顯示plt.cla()plt.plot(sin_list)# plt.draw()也可以放在這個(gè)位置,不會(huì)阻塞 plt.draw()12.使圖片可以手動(dòng)保存
????前面畫(huà)的圖,我們只能copy,不能自己手動(dòng)保存。
添加一行代碼import matplotlib; matplotlib.use('TkAgg') 即可實(shí)現(xiàn)
????不加這行代碼
????加
import numpy as np import matplotlib.pyplot as plt import matplotlib; matplotlib.use('TkAgg') from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False x=np.linspace(-2*np.pi,2*np.pi,400) siny=np.sin(x) cosy=np.cos(x)plt.plot(x,siny,color="red",label="sin(x)") plt.plot(x,cosy,color="blue",label="cos(x)",linestyle="--") plt.xlabel("輸入數(shù)據(jù) x") plt.ylabel("sin(x) 或者 cos(x)") plt.title("三角函數(shù)圖") plt.legend() plt.show()然后我們可以手動(dòng)保存,可以保存為不同格式。
13.調(diào)整坐標(biāo)軸刻度大小
在繪圖里加一行代碼即可
14.高級(jí)三維 曲線圖
import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = Falsefig=plt.figure(figsize=(10,10))#指定畫(huà)布大小 ax=fig.gca(projection='3d')#指定為3D#子圖1 theta=np.linspace(-4*np.pi,4*np.pi,100) x=np.sin(theta) y=np.cos(theta)ax.plot(np.ones(len(x)),np.arange(len(x)),x) ax.plot(2*np.ones(len(x)),np.arange(len(x)),x) ax.plot(3*np.ones(len(y)),np.arange(len(y)),y) #plt.axis('off')#關(guān)閉所有坐標(biāo)軸 ax.set_xlabel('個(gè)數(shù)') ax.set_ylabel('長(zhǎng)度') ax.set_zlabel('數(shù)') plt.legend() plt.show()
作者:電氣余登武
總結(jié)
以上是生活随笔為你收集整理的python matplotlib 绘图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 工行贷款怎么贷利息高吗
- 下一篇: 模拟退火算法求解旅行商问题(python