Commoner
Commoner

Reputation: 1768

Matplotlib Subplot - Unexpected Y axis ticks

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.

Upvotes: 0

Views: 599

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339052

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

Upvotes: 1

Related Questions