New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hide "inner" {x,y}labels in label_outer too. #6619
Conversation
`ax.label_outer` used to hide only "inner" ticklabels; make it hide axes labels too. (I couldn't find a way to just switch label visibility, so I just set the labels to the empty string instead.)
@anntzer I didn't find an example of """
When plotting a grid of subplots that share axes, it might be interesting
to display only some of the tick labels as well as some of the axes labels.
The `label_outer` method provides a convenient way of dealing with this
by setting visible only the x tick and axis labels of the bottom row and
the y tick and axis labels of the left column.
"""
import matplotlib.pyplot as plt
import numpy as np
# Dummy data to plot
t = np.linspace(0.0, 5.0, 501)
s1 = 1.25 * np.sin(2*np.pi*t)
s2 = np.exp(-t)
s3 = np.sin(4*np.pi*t)
s4 = np.sin(2*np.pi*t) * np.exp(-t)
# Instantiate a 2x2 subplot grid with both shared x and y axes.
fig, axs = plt.subplots(ncols=2, nrows=2, sharex=True, sharey=True)
# Plot a data signal in each subplot and display:
# - y tick and axis labels only for the first column;
# - x tick and axis labels only for the last row.
for ax, s, title in ((axs[0, 0], s1, 'Left labels ON'),
(axs[0, 1], s2, 'All labels OFF'),
(axs[1, 0], s3, 'Left & bottom labels ON'),
(axs[1, 1], s4, 'Bottom labels ON')):
ax.plot(t, s)
ax.set_xlabel('This is x-axis')
ax.set_ylabel('This is y-axis')
ax.label_outer() # Display only the "relevant" tick & axis labels.
ax.set_title(title)
plt.show() if one wants to add a simple stand-alone example. And if one doesn't like such a small stand-alone example, then the example subplots_demo may be a good place to add the same kind of example. For the moment (without the current PR), the provided snippet results in |
I updated |
Nice! In fact, I hadn't directly proposed to modify the existing example in the case someone thought it would be useful to keep an example of the “cosmetricks” like plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False) which might give more fine-tuning possibilities if one doesn't simply want to label only the “outer” subplots. However if one had to choose, as |
If I wasn't clear in my previous comment: I agree with you about |
ax.label_outer
used to hide only "inner" ticklabels; make it hide axeslabels too. (I couldn't find a way to just switch label visibility, so
I just set the labels to the empty string instead.)