python的matplotlib绘图(双坐标轴)

2023-05-01 15:27:15 fuzzy 6

python的matplotlib绘图(双坐标轴)

绘制图形如下:

自动交易者

代码如下:

  1. import pandas as pd

  2. import matplotlib.pyplot as plt

  3. from pylab import mpl


  4. ###读取文件

  5. df=pd.read_excel('file')


  6. ###创建画布和绘图区域

  7. fig = plt.figure(figsize=(10,6)) 

  8. ax1 = fig.add_subplot(111)  

  9. ###使图像中的文字正常显示

  10. mpl.rcParams['font.sans-serif']=['Microsoft YaHei']


  11. ###绘图

  12. ax1.bar(d['A'],d['B'],linewidth=2,label='B',color='Black')

  13. ax1.bar(d['A'],d['C'],linewidth=2,label='C',color='red')

  14. ax2=ax1.twinx()

  15. ax2.plot(d['A'],d['D'],linewidth=2,label='D',color='B')

  16. ###添加图例

  17. ax1.legend(loc='upper left')

  18. ax2.legend(loc='upper right')

  19. ####添加X,Y坐标轴

  20. ax1.set_xlabel("Hour")

  21. ax1.set_ylabel("B、C,(μg/m^3)")

  22. ax2.set_ylabel("OC/EC")


  23. ####添加标题


  24. ###设置坐标轴范围

  25. ax2.set_ylim(0, 2)

  26. ax1.set_ylim(0,200)

  27. ####设置坐标轴X的间隔及显示字体大小

  28. plt.xticks([0,2,4,6,8,10,12,14,16,18,20,22],fontsize=20)


  29. ####保存文件

  30. plt.savefig('***02.jpg')

  31. plt.show()


matplotlib官方文档:
https://matplotlib.org/index.html

原文链接

python的matplotlib绘图(双坐标轴)_python 画两条横坐标不同的线_Xinrui__的博客-CSDN博客


首页
应用
追剧
资讯
联系