Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can set the horizontal gridlines based on the right y-axis with 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

enter image description here

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ax.grid(axis = "y")

How can do it with matplotlib then?

>Solution :

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()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading