Skip to content

plotting: passing in ax is removing the wrong x labels #9737

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

Closed
jankatins opened this issue Mar 26, 2015 · 6 comments · Fixed by #9740
Closed

plotting: passing in ax is removing the wrong x labels #9737

jankatins opened this issue Mar 26, 2015 · 6 comments · Fixed by #9740
Labels
Milestone

Comments

@jankatins
Copy link
Contributor

The following code will show the x labels on the right side, but actually it should show the labels at the bottom (or better: should show all!)

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec
import pandas as pd
gs = mpl.gridspec.GridSpec(2, 2)
ax_tl = plt.subplot(gs[0,0])
ax_ll = plt.subplot(gs[1,0])
ax_tr = plt.subplot(gs[0,1])
ax_lr = plt.subplot(gs[1,1])

axes = [ax_tl, ax_ll, ax_tr, ax_lr]

df = pd.DataFrame({"a":[1,2,3,4,5,6], "b":[1,2,3,4,5,6]})

for ax in axes:
    df.plot(x="a", y="b", title="title", ax=ax)
gs.tight_layout(plt.gcf())

IMO: in MPLPlot.__init__()

def __init__(..., sharex=None,...):
    [...]
    if sharex is None: 
        if not ax is None:
             # if we get a ax, the users should know what he does... 
             sharex=False
        else:
             sharex = True
    [...]
@TomAugspurger TomAugspurger added the Visualization plotting label Mar 27, 2015
@TomAugspurger TomAugspurger added this to the 0.16.1 milestone Mar 27, 2015
@jankatins
Copy link
Contributor Author

While I still think that the highest plotting command should refuse to handle sharex and sharey together with not ax is None (e.g. "if we don't build the axis, don't mess with them"), the following fix would at least do the right thing:

in _handle_shared_axes:

def _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey):
    if nplots > 1:

        if sharex and nrows > 1:
-            for ax in axarr[:naxes][:-ncols]:    # only bottom row
+            for ax in axarr:
+                # only the last row of subplots should get x labels -> all other off
+                if not ax.is_last_row():
+                    continue
[...]
        if sharey and ncols > 1:
            for i, ax in enumerate(axarr):
-                if (i % ncols) != 0:  # only first column
+                # only the first column should get y labels -> set all other to off
+                if not ax.is_first_col(): 
[...]

See this example:

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.gridspec as gridspec
import pandas as pd
gs = mpl.gridspec.GridSpec(2, 2)
ax_tl = plt.subplot(gs[0,0])
ax_ll = plt.subplot(gs[1,0])
ax_tr = plt.subplot(gs[0,1])
ax_lr = plt.subplot(gs[1,1])

axes = [ax_tl, ax_ll, ax_tr, ax_lr]

df = pd.DataFrame({"a":[1,2,3,4,5,6], "b":[1,2,3,4,5,6]})

for ax in axes:
    df.plot(x="a", y="b", title="title", ax=ax, sharex=False)

for ax in plt.gcf().get_axes():
    if not ax.is_last_row():
        for label in ax.get_xticklabels():
            label.set_visible(False)
        ax.xaxis.get_label().set_visible(False)

gs.tight_layout(plt.gcf())

@jankatins
Copy link
Contributor Author

I can do a PR with this two changes (adding the "sharex|y together with ax" check and using "is_last_row/first_col"), if someone says that this is the right way forward.

@jankatins
Copy link
Contributor Author

Ok, just did that and uploaded in #9740

@TomAugspurger
Copy link
Contributor

Thanks. I'll try to give it a look this weekend.

@TomAugspurger
Copy link
Contributor

Closed by #9740

@jankatins
Copy link
Contributor Author

Closing, as the PR is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants