如何在matplotlib中根据右侧y轴设置水平网格线?

huangapple go评论67阅读模式
英文:

How can set the horizontal gridlines based on the right y-axis with matplotlib?

问题

我们可以使用Matlab来基于右Y轴设置网格:

如何在matplotlib中根据右侧y轴设置水平网格线?

您可以看到水平网格线是基于右Y轴而不是左Y轴的。在Matplotlib中,ax.grid(axis="y")会绘制基于左Y轴的水平网格线。

那么,如何在Matplotlib中实现呢?

英文:

We can set the grid based on the right y-axis with matlab:
https://www.mathworks.com/matlabcentral/answers/671503-how-to-set-the-grid-of-the-right-yyaxis

如何在matplotlib中根据右侧y轴设置水平网格线?

You see that the horizontal gridlines based on the right y-axis ,instead of the left y-axis.In matplotlib,ax.grid(axis = "y") draw the horizontal gridlines based on the left y-axis.

ax.grid(axis = "y")

How can do it with matplotlib then?

答案1

得分: 1

由于您有两个y轴,您可以使用ax2.grid()来设置次要轴的网格为真。示例代码如下:

fig, ax=plt.subplots(figsize=(7,7))
ax1=ax.twinx()
ax1.set_ylim(-1,3)
ax1.grid()
ax.xaxis.grid(True)
plt.show()

如何在matplotlib中根据右侧y轴设置水平网格线?

英文:

As you have two y-axis, you can set the secondary axis grid to true using ax2.grid(). Working example...

fig, ax=plt.subplots(figsize=(7,7))
ax1=ax.twinx()
ax1.set_ylim(-1,3)
ax1.grid()
ax.xaxis.grid(True)
plt.show()

如何在matplotlib中根据右侧y轴设置水平网格线?

huangapple
  • 本文由 发表于 2023年7月3日 19:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定