(1)、导入库
import matplotlib.pyplot as plt
import numpy
(2)、figure对象和subplot简单运用
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
from numpy.random import randn
plt.plot(randn(50).cumsum(),'k--')
ax1.hist(randn(100),bins=20,color='k',alpha=0.3)
(3)、调整subplot周围的间距
fig,axes = plt.subplots(2,2,sharex=True,sharey=True)
for i in range(2):
for j in range(2):
axes[i, j ].hist(numpy.random.randn(500),bins = 50,color='k',alpha=0.5)
plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=0,hspace=0)
(4)、颜色、标记和线型
plt.figure()
plt.plot(numpy.random.randn(30).cumsum(),linestyle='--',color='g',marker='o')
(5)、设置标题、轴标签,刻度以及刻度标签
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum())
ax.set_xticks([0,250,500,750,1000])
ax.set_yticks([-20,-10,0,10,20])
ax.set_title('My first matplotlib plot')
ax.set_xlabel('Xtages')
ax.set_ylabel('Ytages')
(6)、添加图例
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one')
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='two')
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='three')
ax.legend(loc='best')
(7)、添加注释
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(numpy.random.randn(1000).cumsum(),'k',label='one')
plt.annotate("Important value", (55,20), xycoords='data',
xytext=(5, 38),
arrowprops=dict(arrowstyle='->'))
(8)、绘制常用图形
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3)
circ = plt.Circle((0.7,0.2),0.15,color='b',alpha=0.3)
ax.add_patch(rect)
ax.add_patch(circ)
(9)、图表的导出
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((0.2,0.75),0.4,0.15,color='k',alpha=0.3)
ax.add_patch(rect)
fig.savefig('figpath.png',dpi = 400,bbox_inches='tight')