0

I am working with matplotlib subplots. This is the skeleton of my code:

import matplotlib.pyplot as plt
from matplotlib import gridspec

plt.close('all')

f, axarr = plt.subplots(2, sharex=True,)
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) 

axarr[0] = plt.subplot(gs[0])   
axarr[1] = plt.subplot(gs[1]) 

axarr[0].set_ylim([-10,10])
axarr[1].set_ylim([-1,1])

plt.tight_layout()
f.subplots_adjust(hspace=0)
plt.show()

This is the output that I get from this code.enter image description here

As one can see, in the left y-axis, I get ytick labels which overlap on top of each other and 'weird' y-axis tick labels (0) in the y-axis on the right hand side. How can I solve this? I will be thankful to have help here.

1 Answer 1

1

Those are the x labels of the upper subplot which are only partially hidden by the lower subplot. Turn them off if you like,

axarr[0].set_xticklabels([])

In order for the ticklabels not to overlap you may change the ylimits of the axes,

axarr[0].set_ylim([-10.5,10])
axarr[1].set_ylim([-1,1.2])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked. The ytick label (0) on the right had side has disappeared now after I used your suggestion. But, in the y-axis on the left, the tick labels still overlap. Is there some method to prevent the overlap of tick labels on the left y-axis?
How would you like to have it instead? Show only one of those labels (which one?) or remove both? Or change the axes limits, such that the labels do not appear at the edge of the axes?
It will be good if I am able to change the axes limits so that the labels do not appear at the edges of the axes. Much thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.