首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在matplotlib中使用相同的起点而不是两个来设置两个y轴图的轴

如何在matplotlib中使用相同的起点而不是两个来设置两个y轴图的轴
EN

Stack Overflow用户
提问于 2020-09-27 18:16:26
回答 1查看 61关注 0票数 0

我正在尝试在matplotlib中绘制两轴曲线图。我的图中的两个y轴在图形中使用不同的起点,我如何纠正它,使两个y轴图使用相同的轴点。

我用来绘制的代码是

代码语言:javascript
复制
fig = plt.figure(figsize=(30, 20))
ax = fig.add_subplot()
ax1 =ax.twinx()
#ax.grid(True,axis='both')
#ax.set(xlabel="Voltage", ylabel="Current", title="IV Curve") 
ax.set_xlabel("Voltage [V]",fontsize = 20)
ax1.set_xlabel("Voltage [V]",fontsize = 20)
ax.set_ylabel("Current [I]",fontsize = 20)
ax1.set_ylabel("Power [W]",fontsize = 20)
ax1.spines['right'].set_color('red')
ax.spines['left'].set_color('red')
ax.get_shared_x_axes().join(ax, ax1)
#ax.set_title("IV/PV Curve Plot",fontweight ='bold', fontsize = 30, color='blue')
ax1.tick_params(axis='both', which='both', labelsize=20)
plt.ylim (0,10) #adjust the current limits
plt.xlim (0,70) #adjust the voltage limits
ax.tick_params(axis='both', which='both', labelsize=20)
plt.ylim (0,400) #adjust the power limits
plt.xlim (0,70) #adjust the voltage limits

ax.plot('Voltage', 'Current', data=df, linewidth=4, label='IV curve')
ax1.plot('Voltage', 'Power', data=df, linewidth=4, color='r', label ='PV Curve')
ax.legend( loc='upper right',fontsize=20)
ax1.legend(loc='lower right',fontsize=20)
#pmax and Vmax

Pmax = df["Power"].max()
maxrow = df[df['Power']==Pmax]
Voltage = maxrow['Voltage'].iloc[0]
Current = maxrow['Current'].iloc[0]
xmax1 =(Voltage/70)
xmax2 =(Current/10)
ax.axhline(y=Current, xmin=0, xmax =xmax1,linewidth=4, color ='g',linestyle='--',)
ax.axvline(x=Voltage, ymin=0, ymax =xmax2, linewidth=4, color='g', linestyle='--')

#img = image.imread("/home/hebin/Desktop/PV/Mitsui/Flasher/Mitsui Logo.png")
#plt.figimage(img, 1380, 1150, alpha=1)
plt.margins(0)
plt.tight_layout()
#plt.savefig('/home/hebin/Desktop/PV/Mitsui/Flasher/IVC/IV.png')

我的输出是这样的

在图像中,您可以看到红线和蓝线从零点开始,但起点不同。我想纠正它,让它从同一个点开始。

EN

回答 1

Stack Overflow用户

发布于 2020-09-27 20:24:23

要在双轴中将零标记为零,请定义以下函数:

代码语言:javascript
复制
def alignZero(ax1, ax2):
    y1Min, y1Max = ax1.get_ylim()
    y2Min, y2Max = ax2.get_ylim()
    ratio1 = -y1Min / (y1Max - y1Min)
    ratio2 = -y2Min / (y2Max - y2Min)
    if ratio2 < ratio1:
        y2newMin = y1Min * y2Max / y1Max
        ax2.set_ylim(y2newMin, y2Max)
    elif ratio2 > ratio1:
        y2newMax = y2Min * y1Max / y1Min
        ax2.set_ylim(y2Min, y2newMax)

然后检查它是如何工作的:

代码语言:javascript
复制
fig = plt.figure()
ax1 = fig.add_subplot()
ax2 = ax1.twinx()
ax1.bar(range(6), (2, -2, 1, 0, 0, 0))
ax2.plot(range(6), (0, 2, 8, -12, 0, 0))
ax1.axhline(0, color='red', linestyle='--', linewidth=1)
alignZero(ax1, ax2)
plt.show()

生成的图片为:

在上面的示例中,需要为ax2 (y2newMax)设置更高的最大值。

进行第二个测试,将ax2.plot(...)中的-12更改为例如-4。这一次,ax2中的最小值(y2newMin)被降低。

要查看没有上述对齐的结果会是什么样子,再做一次测试,并注释掉alignZero的调用(零现在将处于不同的级别)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64087309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档