鱼C论坛

 找回密码
 立即注册
查看: 2469|回复: 1

请问如何使用matplotlib绘制坐标纸

[复制链接]
发表于 2019-4-12 15:55:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
使用matplotlib画伏安特性曲线时要画一张坐标纸我式了一些方法能实现我想要的效果,但感觉代码比较乱,而且idle中运行报错
Warning (from warnings module):
  File "C:\Python37\lib\site-packages\matplotlib\figure.py", line 98
    "Adding an axes using the same arguments as a previous axes "
MatplotlibDeprecationWarning:
Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

但可以继续运行

请问是否有更好的实现方法,谢谢!

我的代码如下:
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.ticker import MultipleLocator, FormatStrFormatter


  4. # 重载配置文件设置字体解决显示中文问题
  5. plt.rcParams['font.sans-serif'] = ['SimHei']
  6. plt.rcParams['font.serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串



  8. plt.xlabel('I电流单位A')# 横坐标名称
  9. plt.ylabel('U电压单位V')# 纵坐标名称


  10. # 设置x轴的取值范围为:-1到2
  11. plt.xlim(0, 0.6)
  12. # 设置y轴的取值范围为:-1到3
  13. plt.ylim(0.8, 2.2)

  14. ax = plt.subplot(111) #注意:一般都在ax中设置,不再plot中设置

  15. #修改主刻度
  16. xmajorLocator = MultipleLocator(0.1)#将x主刻度标签设置为0.1的倍数
  17. xmajorFormatter = FormatStrFormatter('%5.1f') #设置x轴标签文本的格式
  18. ymajorLocator = MultipleLocator(0.5) #将y轴主刻度标签设置为0.5的倍数
  19. ymajorFormatter = FormatStrFormatter('%1.1f') #设置y轴标签文本的格式
  20. #设置主刻度标签的位置,标签文本的格式
  21. ax.xaxis.set_major_locator(xmajorLocator)
  22. ax.xaxis.set_major_formatter(xmajorFormatter)
  23. ax.yaxis.set_major_locator(ymajorLocator)
  24. ax.yaxis.set_major_formatter(ymajorFormatter)

  25. #修改次刻度
  26. xminorLocator = MultipleLocator(0.02) #将x轴次刻度标签设置为5的倍数
  27. yminorLocator = MultipleLocator(0.05) #将此y轴次刻度标签设置为0.1的倍数
  28. #设置次刻度标签的位置,没有标签文本格式
  29. ax.xaxis.set_minor_locator(xminorLocator)
  30. ax.yaxis.set_minor_locator(yminorLocator)

  31. #打开网格
  32. ax.xaxis.grid(True, which='minor') #x坐标轴的网格使用主刻度
  33. ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度


  34. plt.title('路端电压与干路电流的关系')# 图像标题
  35. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-12 21:37:19 | 显示全部楼层
精简了以下代码,并且把ax = plt.subplot()的位置调整了一下就不报错了,但还是感觉不能太理解代码的意思求大神指教。
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.ticker import MultipleLocator, FormatStrFormatter


  4. # 重载配置文件设置字体解决显示中文问题
  5. plt.rcParams['font.sans-serif'] = ['SimHei']
  6. plt.rcParams['font.serif'] = ['SimHei']
  7. plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串

  8. ax = plt.subplot() #注意:一般都在ax中设置,不再plot中设置

  9. plt.xlabel('I电流单位A')# 横坐标名称
  10. plt.ylabel('U电压单位V')# 纵坐标名称


  11. # 设置x轴的取值范围为:0到0.6
  12. plt.xlim(0, 0.6)
  13. # 设置y轴的取值范围为:0.8到2.2
  14. plt.ylim(0.8, 2.2)


  15. #修改次刻度
  16. xminorLocator = MultipleLocator(0.02) #将x轴次刻度标签设置为0.02的倍数
  17. yminorLocator = MultipleLocator(0.05) #将此y轴次刻度标签设置为0.05的倍数

  18. #设置次刻度标签的位置,标签文本格式
  19. ax.xaxis.set_minor_locator(xminorLocator)
  20. ax.yaxis.set_minor_locator(yminorLocator)

  21. #打开网格
  22. ax.xaxis.grid(True, which='minor') #x坐标轴的网格使用主刻度
  23. ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度


  24. plt.title('路端电压与干路电流的关系')# 图像标题
  25. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-15 06:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表