matplotlib 類似於MATALAB,提供多種將數據繪製成圖的方法。
import matplotlib.pyplot as plt
plt.plot([10,5,2,4],color=‘green’,label=‘line 1’,linewidth=5)
plt.ylabel(‘y’,fontsize=40)
plt.xlabel(‘x’,fontsize=40)
plt.axis([0,3,0,15])
plt.show()
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.set_xlabel(‘x’,fontsize=40)
ax.set_ylabel(‘y’,fontsize=40)
fig.suptitle(‘figure’,fontsize=40)
ax.plot([40,5,2.4],color=‘green’,label=‘line 1’,linewidth=5)
fig.savefig(‘figure.png’)
導入plot庫爲plt之後,初始化figure對象(fig),添加axis對象(ax).每條線是通過ax.plot()命令會知道ax對象之中,每條線稱爲句柄(handle).然後matplotlib.pyplot記錄下面所有的指令並將豈會知道figure對象之中
import numpy as np
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
r = np.arange(0.,10.,0.3)
p1, = ax.plot(r,r,'r--',label='line 1', linewidth=10)
p2, = ax.plot(r,r**0.5,'bs',label='line 2', linewidth=10)
p3, = ax.plot(r,np.sin(r),'g^',label='line 3',linewidth=10)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles,labels,fontsize=40)
ax.set_xlabel('x',fontsize=40)
ax.set_ylabel('y',fontsize=40)
fig.suptitle('figure 1',fontsize=40)
fig.savefig('figure_multiplelines.png')
散點圖
colors = ["b", "c", "y", "m", "r"]
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.scatter(np.random.random(10),np.random.random(10),marker='x',color=color[0])
p1 =ax.scatter(np.random.random(10),np.random.random(10),marker='x',color=color[0],s=50)
p2 =ax.scatter(np.random.random(10),np.random.random(10),marker='o',color=color[1],s=50)
p3 =ax.scatter(np.random.random(10),np.random.random(10),marker='o',color=color[2],s=50)
ax.legend((p1,p2,p3),("point1","point2","point3"),fontsize=20)
ax.set_xlabel('x',fontsize(40))
ax.set_ylabel('y',fontsize(40))
fig.savefig('figure_scatterplot.png')