0


python必备库-画图神器Matplotlib手把手教学

文章目录

听说点进蝈仔帖子的都喜欢点赞加关注~~

在这里插入图片描述
官网地址:
https://matplotlib.org/

可以看看docs
在这里插入图片描述
官网就相当详细了,可以直接参考官网。

1.安装方法

pip安装:

  1. pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

conda安装:

  1. conda install matplotlib

测试是否成功:

  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. x = np.arange(1,11)
  4. y =2* x +5
  5. plt.title("Matplotlib demo")
  6. plt.xlabel("x axis caption")
  7. plt.ylabel("y axis caption")
  8. plt.plot(x,y)
  9. plt.show()

成功出现下图就可以动手改造了。
在这里插入图片描述

2.用好官网的例子

最简单的应用-折线图

  1. fig, ax = plt.subplots()# Create a figure containing a single axes.
  2. ax.plot([1,2,3,4],[1,4,2,3]);# Plot some data on the axes.

在这里插入图片描述

添加注释的方法

  1. fig, ax = plt.subplots(figsize=(5,2.7))
  2. t = np.arange(0.0,5.0,0.01)
  3. s = np.cos(2* np.pi * t)
  4. line,= ax.plot(t, s, lw=2)
  5. ax.annotate('local max', xy=(2,1), xytext=(3,1.5),
  6. arrowprops=dict(facecolor='black', shrink=0.05))
  7. ax.set_ylim(-2,2);

在这里插入图片描述

柱状图-Bar Label

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. N =5
  4. menMeans =(20,35,30,35,-27)
  5. womenMeans =(25,32,34,20,-25)
  6. menStd =(2,3,4,1,2)
  7. womenStd =(3,5,2,3,3)
  8. ind = np.arange(N)# the x locations for the groups
  9. width =0.35# the width of the bars: can also be len(x) sequence
  10. fig, ax = plt.subplots()
  11. p1 = ax.bar(ind, menMeans, width, yerr=menStd, label='Men')
  12. p2 = ax.bar(ind, womenMeans, width,
  13. bottom=menMeans, yerr=womenStd, label='Women')
  14. ax.axhline(0, color='grey', linewidth=0.8)
  15. ax.set_ylabel('Scores')
  16. ax.set_title('Scores by group and gender')
  17. ax.set_xticks(ind, labels=['G1','G2','G3','G4','G5'])
  18. ax.legend()# Label with label_type 'center' instead of the default 'edge'
  19. ax.bar_label(p1, label_type='center')
  20. ax.bar_label(p2, label_type='center')
  21. ax.bar_label(p2)
  22. plt.show()

正常run会出现下图
在这里插入图片描述

折线图之CSD

计算两个信号的交叉谱密度Compute the cross spectral density of two signals

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig,(ax1, ax2)= plt.subplots(2,1)# make a little extra space between the subplots
  4. fig.subplots_adjust(hspace=0.5)
  5. dt =0.01
  6. t = np.arange(0,30, dt)# Fixing random state for reproducibility
  7. np.random.seed(19680801)
  8. nse1 = np.random.randn(len(t))# white noise 1
  9. nse2 = np.random.randn(len(t))# white noise 2
  10. r = np.exp(-t /0.05)
  11. cnse1 = np.convolve(nse1, r, mode='same')* dt # colored noise 1
  12. cnse2 = np.convolve(nse2, r, mode='same')* dt # colored noise 2# two signals with a coherent part and a random part
  13. s1 =0.01* np.sin(2* np.pi *10* t)+ cnse1
  14. s2 =0.01* np.sin(2* np.pi *10* t)+ cnse2
  15. ax1.plot(t, s1, t, s2)
  16. ax1.set_xlim(0,5)
  17. ax1.set_xlabel('time')
  18. ax1.set_ylabel('s1 and s2')
  19. ax1.grid(True)
  20. cxy, f = ax2.csd(s1, s2,256,1./ dt)
  21. ax2.set_ylabel('CSD (db)')
  22. plt.show()

在这里插入图片描述


本文转载自: https://blog.csdn.net/weixin_39490300/article/details/123505946
版权归原作者 易烊千蝈 所有, 如有侵权,请联系我们删除。

“python必备库-画图神器Matplotlib手把手教学”的评论:

还没有评论