python学习——Matplotlib库的基础
Matplotlib
简要说明:由jupyter notebook导入,运行结果紧随代码
import matplotlib.pyplot as plt
import numpy as np
from matplotlib_inline import backend_inline
backend_inline.set_matplotlib_formats('svg')
%matplotlib inline
fig1 = plt.figure() # 创建新图窗
x = [1,2,3,4,5]
y = [1,8,27,64,125]
plt.plot(x,y) # 描点连线
plt.bar(x,y)
<BarContainer object of 5 artists>
fig1.savefig(r'D:\Study\test.svg')
fig2 = plt.figure()
# ax2 = plt.axes()
x = [1,2,3,4,5]
y1 = [1,2,3,4,5]
y2 = [0,0,0,0,0]
y3 = [-1,-2,-3,-4,-5]
plt.plot(x,y1,label='y1')
plt.plot(x,y2,label='y2')
plt.plot(x,y3)
plt.legend(frameon=False,ncol=2,loc='best')
plt.grid(color='red',linestyle='--')
# ax2.plot(x,y1)
# ax2.plot(x,y2)
# ax2.plot(x,y3)
fig3 = plt.figure()
x = [1,2,3,4,5]
y1 = [1,2,3,4,5]
y2 = [0,0,0,0,0]
y3 = [-1,-2,-3,-4,-5]
plt.subplot(3,1,1),plt.plot(x,y1)
plt.subplot(3,1,2),plt.plot(x,y2)
plt.subplot(3,1,3),plt.plot(x,y3)
(<Axes: >, [<matplotlib.lines.Line2D at 0x22df0185400>])
fig4 = plt.figure()
x = [1,2,3,4,5]
y1 = [1,2,3,4,5]
y2 = [0,0,0,0,0]
y3 = [-1,-2,-3,-4,-5]
y4 = [2,3,4,5,6]
y5 = [-2,-3,-4,-5,-6]
plt.plot(x,y1,color='red',linestyle='-',linewidth=4,marker='o',markersize=5)
plt.plot(x,y2,color='blue',linestyle='--', linewidth=1.5,marker='s',markersize=5)
plt.plot(x,y3,color='green',linestyle='-.', linewidth=1, marker='D',markersize=5)
plt.plot(x,y4,color='yellow',linestyle=':', linewidth=1.5,marker='.',markersize=5)
plt.plot(x,y5,color='purple',linestyle='', linewidth=3,marker='^',markersize=5) # 绘制散点图
plt.legend(['y1','y2','y3','y4','y5'])
<matplotlib.legend.Legend at 0x22df6012400>
x6 = np.linspace(0,10,1000)
I = np.sin(x6)*np.cos(x6).reshape(-1,1)
fig5 = plt.figure()
plt.imshow(I)
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x22df11e0f10>
x11 = np.random.normal(3,1,1000)
x12 = np.random.normal(6,1,1000)
x13 = np.random.normal(9,1,1000)
fig6 = plt.figure()
plt.hist(x11,bins=30,alpha=1,histtype='stepfilled',color='blue',edgecolor='yellow',linewidth=2)
plt.hist(x12,bins=30,alpha=0.5,histtype='stepfilled',color='red',edgecolor='yellow',linewidth=2)
plt.hist(x13,bins=30,alpha=0.5,histtype='stepfilled',color='purple',edgecolor='yellow',linewidth=2)
(array([ 3., 3., 6., 8., 12., 16., 29., 42., 49., 51., 67., 71., 76.,95., 89., 60., 77., 59., 54., 43., 33., 22., 7., 10., 8., 1.,5., 2., 0., 2.]),array([ 6.04959894, 6.2639757 , 6.47835246, 6.69272921, 6.90710597,7.12148273, 7.33585949, 7.55023624, 7.764613 , 7.97898976,8.19336652, 8.40774327, 8.62212003, 8.83649679, 9.05087355,9.26525031, 9.47962706, 9.69400382, 9.90838058, 10.12275734,10.33713409, 10.55151085, 10.76588761, 10.98026437, 11.19464112,11.40901788, 11.62339464, 11.8377714 , 12.05214815, 12.26652491,12.48090167]),[<matplotlib.patches.Polygon at 0x22df5415550>])
fig7 = plt.figure() # 创建新图窗
x22 = [1,2,3,4,5]
y22 = [1,8,27,64,125]
plt.plot(x22,y22) # 描点连线
plt.xlim(1,5)
plt.ylim(1,100)
plt.title('This is the title')
plt.xlabel('This is the xlabel')
plt.ylabel('This is the ylabel')
# plt.axis([1,5,1,50])
#plt.axis('equal')
Text(0, 0.5, 'This is the ylabel')