|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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.
但可以继续运行
请问是否有更好的实现方法,谢谢!
我的代码如下:
- import numpy as np
- import matplotlib.pyplot as plt
- from matplotlib.ticker import MultipleLocator, FormatStrFormatter
- # 重载配置文件设置字体解决显示中文问题
- plt.rcParams['font.sans-serif'] = ['SimHei']
- plt.rcParams['font.serif'] = ['SimHei']
- plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串
- plt.xlabel('I电流单位A')# 横坐标名称
- plt.ylabel('U电压单位V')# 纵坐标名称
- # 设置x轴的取值范围为:-1到2
- plt.xlim(0, 0.6)
- # 设置y轴的取值范围为:-1到3
- plt.ylim(0.8, 2.2)
- ax = plt.subplot(111) #注意:一般都在ax中设置,不再plot中设置
- #修改主刻度
- xmajorLocator = MultipleLocator(0.1)#将x主刻度标签设置为0.1的倍数
- xmajorFormatter = FormatStrFormatter('%5.1f') #设置x轴标签文本的格式
- ymajorLocator = MultipleLocator(0.5) #将y轴主刻度标签设置为0.5的倍数
- ymajorFormatter = FormatStrFormatter('%1.1f') #设置y轴标签文本的格式
- #设置主刻度标签的位置,标签文本的格式
- ax.xaxis.set_major_locator(xmajorLocator)
- ax.xaxis.set_major_formatter(xmajorFormatter)
- ax.yaxis.set_major_locator(ymajorLocator)
- ax.yaxis.set_major_formatter(ymajorFormatter)
- #修改次刻度
- xminorLocator = MultipleLocator(0.02) #将x轴次刻度标签设置为5的倍数
- yminorLocator = MultipleLocator(0.05) #将此y轴次刻度标签设置为0.1的倍数
- #设置次刻度标签的位置,没有标签文本格式
- ax.xaxis.set_minor_locator(xminorLocator)
- ax.yaxis.set_minor_locator(yminorLocator)
- #打开网格
- ax.xaxis.grid(True, which='minor') #x坐标轴的网格使用主刻度
- ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度
- plt.title('路端电压与干路电流的关系')# 图像标题
- plt.show()
复制代码 |
|