Skip to content
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

Merged
merged 2 commits into from Jul 12, 2016

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Jun 21, 2016

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

`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.)
@tacaswell tacaswell added this to the 2.1 (next point release) milestone Jun 21, 2016
@afvincent
Copy link
Contributor

@anntzer I didn't find an example of ax.label_outer in the example gallery so wouldn't your PR be the occasion to add one? For example something like:

"""
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
example_label_outer
but this should improve with what you proposed.

@anntzer
Copy link
Contributor Author

anntzer commented Jun 21, 2016

I updated subplots_demo instead.

@afvincent
Copy link
Contributor

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 label_outer will now also handle “inner” axis labels (as well as it also takes care of the possible offset texts), which the former “cosmetrick” doesn't, I really think label_outer should be demonstrated over the former technic.

@anntzer
Copy link
Contributor Author

anntzer commented Jun 21, 2016

Actually the previous approach is "broken" in the sense that it will not handle offset texts (see #4445, #4446), so I'd rather just not have it in the examples here.

@afvincent
Copy link
Contributor

If I wasn't clear in my previous comment: I agree with you about label_outer being a better solution than the former approach because it takes care of the offset texts (as well as of the x- and y-labels with this PR).

@tacaswell tacaswell merged commit 4786a0b into matplotlib:master Jul 12, 2016
@anntzer anntzer deleted the labelouter-hide-labels branch December 1, 2016 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants