matplotlib小学笔记

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')

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章