最近手上有需要用matplotlib画图的活,在网上淘了本实践书,发现没有代码,于是手敲了一遍,mark下。
第一篇 第一章
图1.1
import matplotlib.pyplot as plt import numpy as np from matplotlib import cm as cm #define data x=np.linspace(0.5, 3.5, 100) y=np.sin(x) y1=np.random.randn(100) #scatter figure plt.scatter(x, y1, c='0.25', label='scatter figure') #plot figure plt.plot(x, y, ls='--', lw=2, label='plot figure') #some clean up #去掉上边框和有边框 for spine in plt.gca().spines.keys(): if spine=='top' or spine=='right': plt.gca().spines[spine].set_color('none') # x轴的刻度在下边框 plt.gca().xaxis.set_ticks_position('bottom') # y轴的刻度在左边框 plt.gca().yaxis.set_ticks_position('left') #设置x轴、y轴范围 plt.xlim(0.0, 4.0) plt.ylim(-3.0, 3.0) #设置x轴、y轴标签 plt.xlabel('x_axis') plt.ylabel('y_axis') #绘制x、y轴网格 plt.grid(True, ls=':', color='r') #绘制水平参考线 plt.axhline(y=0.0, c='r', ls='--', lw=2) #绘制垂直参考区域 plt.axvspan(xmin=1.0, xmax=2.0, facecolor='y', alpha=0.5) #绘制注解 plt.annotate('maximum', xy=(np.pi/2, 1.0), xytext=((np.pi/2)+0.15, 1.5), weight='bold', color='r', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r')) #绘制注解 plt.annotate('spines', xy=(0.75, -3), xytext=(0.35, -2.25), weight='bold', color='r', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r')) #绘制注解 plt.annotate('', xy=(0, -2.78), xytext=(0.4, -2.32), weight='bold', color='r', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r')) #绘制注解 plt.annotate('', xy=(3.5, -2.98), xytext=(3.6, -2.7), weight='bold', color='r', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r')) #绘制文本 plt.text(3.6, -2.70, "'|' is tickline", weight='bold', color='b') plt.text(3.6, -2.95, "3.5 is tickline", weight='bold', color='b') plt.title("structure of matplotlib") plt.legend(loc='upper right') plt.show()
=======================================================
图 1.2
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.cos(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.legend() plt.show()
=======================================================
图 1.3
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.random.rand(1000) plt.scatter(x,y,label='scatter figure') plt.legend() plt.show()
=======================================================
图1.4
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.random.rand(1000) plt.scatter(x,y,label='scatter figure') plt.legend() plt.xlim(0.05, 10) plt.ylim(0, 1) plt.show()
=======================================================
图 1.5
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.xlabel('x-axis') plt.ylabel('y-axis') plt.legend() plt.show()
=======================================================
图 1.6
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.grid(linestyle=':', color='r') plt.legend() plt.show()
=======================================================
图 1.7
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.axhline(y=0.0, c='r', ls='--', lw=2) plt.axvline(x=4.0, c='r', ls='--', lw=2) plt.legend() plt.show()
=======================================================
图 1.8
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.axvspan(xmin=4.0, xmax=6.0, facecolor='y', alpha=0.3) plt.axhspan(ymin=0.0, ymax=0.5, facecolor='y', alpha=0.3) plt.legend() plt.show()
=======================================================
图 1.9
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.annotate('maximum', xy=(np.pi/2, 1.0), xytext=((np.pi/2)+1.0, 0.8),weight='bold', color='b', arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='b')) plt.legend() plt.show()
=======================================================
图 1.10
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.text(3.1, 0.09, 'y=sin(x)', weight='bold', color='b') plt.legend() plt.show()
=======================================================
图 1.11
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.title("y=sin(x)") plt.legend() plt.show()
=======================================================
图 1.12
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0.05, 10, 1000) y=np.sin(x) plt.plot(x,y,ls='-.', lw=2, c='c', label='plot figure') plt.legend(loc="lower right") plt.show()