7

I am using GridSpec to plot two plots one below the other without a gap in between with

gs = gridspec.GridSpec(3, 1)
gs.update(hspace=0., wspace=0.)
ax1 = plt.subplot(gs[0:2, 0])
ax2 = plt.subplot(gs[2, 0], sharex=ax1)

which works fine. However, I want to get rid of each subplot's top and bottom tick label. For that I use

nbins = len(ax1.get_yticklabels())
ax1.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='both'))
nbins = len(ax2.get_yticklabels())
ax2.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='both'))

which in many cases works fine. In some plots, however, one or more of the 4 labels to prune are still there. I looked at e.g. ax1.get_ylim() and noticed that instead of for example the upper limit being 10 (as it is shown in the plot itself), it is actually 10.000000000000002, which I suspect is the reason why it is not pruned. How does that happen and how can I get rid of that?

Here is an example: Note that in the figure the y axis is inverted and no label is pruned, altough it should be. Also note that for some reason the lowest y-label is set to a negative position, which I don't see. The y-tick positions are shown in in axis coordinates in the text within the plots. In the image below, the label at 10.6 should not be there!

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
import numpy as np

x1 = 1
y1 = 10.53839
err1 = 0.00865
x2 = 2
y2 = 9.43045
err2 = 0.00658

plt.clf()
fig = plt.figure(figsize=(6, 6))
gs = gridspec.GridSpec(3, 1)
gs.update(hspace=0., wspace=0.)
ax1 = plt.subplot(gs[0:2, 0])

ax1.errorbar(x1, y1, yerr=err1)
ax1.errorbar(x2, y2, yerr=err2)

ax1.invert_yaxis()

plt.setp(ax1.get_xticklabels(), visible=False)  # Remove x-labels between the plots
plt.xlim(0, 3)

ax2 = plt.subplot(gs[2, 0], sharex=ax1)

nbins = len(ax1.get_yticklabels())
ax1.yaxis.set_major_locator(MaxNLocator(nbins=8, prune='both'))
nbins = len(ax2.get_yticklabels())
ax2.yaxis.set_major_locator(MaxNLocator(nbins=6, prune='both'))

plt.savefig('prune.png')
plt.close()

prune

7
  • 1
    From your question, it sounds like you want to have the same x-axis, and so no tick labels at all fro that one, covered in the answer below, and then you just want to prune lower and upper, for the upper and lower graphs, respectively. Is that an accurate interpretation?
    – will
    Commented Jan 13, 2015 at 8:55
  • Exactly, the x axis is just fine. I want to set the number of y-ticks (in the example above nbins,) and from that I want to prune the upper and lower value, if it is locate at the (lower or upper) end of the axis.
    – frixhax
    Commented Jan 13, 2015 at 9:47
  • I can not reproduce the error. With the code you provided (including the line in my answer below) everything works as it should for me. Could you provide an example that shows the problem?
    – hitzg
    Commented Jan 13, 2015 at 10:29
  • Yes, I will try to make a minimal example and get back to you.
    – frixhax
    Commented Jan 13, 2015 at 10:35
  • I see the problem now. However, your example is still very large. Could you try to remove everything that is irrelevant to the question?
    – hitzg
    Commented Jan 15, 2015 at 9:48

2 Answers 2

2

Could it be, that you are looking at the left most label on the x axis of the upper plot? If so, this should do the trick:

ax1.set_xticklabels([])

EDIT: If you use sharex, you have to use this, otherwise the tick labels are removed on both axes.

plt.setp(ax1.get_xticklabels(), visible=False)
4
  • 1
    No, I don't use any x labels between the two plots, hence hspace=0. what prune is supposed to do is to prune the highest and lowest tick label, if it is located at either end of the y-axis. But in many cases it just doesn't do that.
    – frixhax
    Commented Jan 13, 2015 at 10:01
  • From your code it was not clear, that you already removed the x tick labels.
    – hitzg
    Commented Jan 13, 2015 at 10:28
  • 1
    Yes, I see, sorry about that. However, for the pruning problem the x axis is unimportant.
    – frixhax
    Commented Jan 13, 2015 at 10:30
  • Thanks, I know, that is what I've been using, of course. I edited the question to include an example.
    – frixhax
    Commented Jan 13, 2015 at 15:36
0

You can try to use this:

import matplotlib.ticker as mticker

ax2.yaxis.set_major_locator(mticker.MaxNLocator(nbins=7, prune='upper'))

I found the above command only works for the y-axis.

Does someone know how to set up the maximum limits of x-axis tickers' number?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.