Search code examples
pythonpython-2.7matplotlibaxis-labelssubplot

How to customize individual numbers on an axis in matplotlib?


I'm trying to overlay these two plots, and so far I think I might have it but the first number on the top y-axis and last number on the bottom y-axis are overlapping each other. Is there a way to individually edit each number on the y-axis so I can delete one of the two, as well make each one different from the other?

enter image description here

This plot ^^ is from the same code below but without the line plt.setp([a.get_yticklabels() for a in plot.axes[:-1]], visible=False)

##Plotting
plt.close('all')
pressure_plot, dyn = plt.subplots(2, sharex = True)
dyn[0].plot(s,p1_dyn,
         s, p2_dyn)
dyn[1].plot(s,A)
plot.subplots_adjust(hspace = 0)
plt.setp([a.get_yticklabels() for a in plot.axes[:-1]], visible=False)

I was thinking it might be possible to use this last line here ^^ to delete one, because if you change it to yticklabels() it will remove a y axis (like seen below). But I'm not sure how to get it to individually delete one. Any ideas?

enter image description here


Solution

  • You can delete the top y value of the axis by doing:

    yticks = plt.gca().get_yticks().tolist() # get list of ticks
    yticks[-1] = ''                          # set last tick to empty string
    ax.set_yticklabels(yticks)