I want to change xlimits and xticks in one subplot of many subplots, but it only works for xticks.
I am plotting many plots, and almost all of them have the same x axis, in one figure and therefore have decided to use plt.subplots(sharex=True)
. For one plot I want different limits and ticks on my x axis. To do this, I remove this plot from the other sharex plots with ax.get_shared_x_axes().remove(ax)
.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(4,10,sharey=True, sharex=True)
axes_flat = axes.flat
for i in range(0,33) :
xs = axes_flat[i]
xs.plot([0,1,2],[2,3,4])
xs.set_xticks([0,2])
xs.set_xlim([0,4])
#leave some room between normal plots and plots with different xlims:
axes_flat[34].axis('off')
axes_flat[35].axis('off')
# Remove this plot from the shared axes and change xlims and xticks:
axes_flat[36].get_shared_x_axes().remove(axes_flat[36])
axes_flat[36].set_xticks([0,1])
axes_flat[36].set_xlim([0,2])
axes_flat[38].axis('off')
axes_flat[39].axis('off')
plt.show()
This works as expected for the x limits but not for the xticks. Changing the ticks in one subplot overwrites all the other ticks, while the x limits are only changed in the subplot, which I wanted to. I do not understand why set_xticks
and set_xlim
would behave differently in this case. Is there a way to fix this, while still using plt.subplots(sharex=True)
?