《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第一章(利用matplotlib做数据可视化)

网友投稿 422 2022-09-03


《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第一章(利用matplotlib做数据可视化)

最近手上有需要用matplotlib画图的活,在网上淘了本实践书,发现没有代码,于是手敲了一遍,mark下。

第一篇    第一章

图1.1

import matplotlib.pyplot as pltimport numpy as npfrom matplotlib import cm as cm#define datax=np.linspace(0.5, 3.5, 100)y=np.sin(x)y1=np.random.randn(100)#scatter figureplt.scatter(x, y1, c='0.25', label='scatter figure')#plot figureplt.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()

View Code

=======================================================

图 1.2

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图 1.3

import matplotlib.pyplot as pltimport numpy as npx=np.linspace(0.05, 10, 1000)y=np.random.rand(1000)plt.scatter(x,y,label='scatter figure')plt.legend()plt.show()

View Code

=======================================================

图1.4

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图 1.5

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图  1.6

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图  1.7

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图 1.8

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图 1.9

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图  1.10

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图  1.11

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code

=======================================================

图  1.12

import matplotlib.pyplot as pltimport numpy as npx=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()

View Code


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot结合Flowable实现工作流开发
下一篇:《Python数据可视化之matplotlib实践》 源码 第四篇 扩展 第十二章(利用matplotlib做数据可视化)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~