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

Sharing axes #9

Closed
spencerkclark opened this issue Jan 1, 2018 · 1 comment · Fixed by #18
Closed

Sharing axes #9

spencerkclark opened this issue Jan 1, 2018 · 1 comment · Fixed by #18

Comments

@spencerkclark
Copy link
Owner

spencerkclark commented Jan 1, 2018

Sharing axes is a really convenient feature of matplotlib. It would be handy if this was enabled in facets. It's not necessarily trivial, but I think it is possible to enable axes sharing on existing axes.

Background

Maybe there are some properties of the axes that might be missing, but simply automatically making inner ticklabels invisible, and sharing tick positions and axes limits would go a long way. In doing some searching, I found this StackOverflow question / answer, which is helpful for sharing axes limits.

Then in doing some reading of the matplotlib source code, namely the following lines: https://github.com/matplotlib/matplotlib/blob/a8ae66ea275b402e3461d69b19d049fc94a34b68/lib/matplotlib/figure.py#L1214-L1226, it seems it shouldn't be too hard to share ticks across axes while keeping some ticklabels visible and others invisible.

Proof of concept

The following is a modified version of the first example in the README (note this works!):

rows, cols = 2, 3
fig, axes = facets(rows, cols, width=8., aspect=0.6,
                   internal_pad=0.2, top_pad=0.5,
                   bottom_pad=0.5, left_pad=0.5, 
                   right_pad=0.5)

x = np.linspace(0., 6. * np.pi)
y = np.sin(x)

# Share ticks and limits of all x and y axes
# Make inner ticklabels invisible
# This could all be done in the facets function
ax_ref = axes[0]
axes = np.reshape(axes, (rows, cols))
for ax in axes.flatten():
    ax.xaxis.major = ax_ref.xaxis.major
    ax.xaxis.minor = ax_ref.xaxis.minor
    ax.yaxis.major = ax_ref.yaxis.major
    ax.yaxis.minor = ax_ref.yaxis.minor
    ax.get_shared_x_axes().join(ax_ref, ax)
    ax.get_shared_y_axes().join(ax_ref, ax)
    
for ax in axes[:-1, :].flatten():
    ax.xaxis.set_tick_params(which='both', 
                             labelbottom=False, labeltop=False)
    
for ax in axes[:, 1:].flatten():
    ax.yaxis.set_tick_params(which='both', 
                             labelbottom=False, labeltop=False)
    
axes = axes.flatten()

# Besides calling facets, this is all the user would need to see
for ax in axes:
    ax.plot(x, y)

axes[0].set_ylim(-2, 2)
axes[0].set_yticks(np.arange(-2, 2.1))
axes[0].set_xticks(np.arange(0., 18.1, 6.))
axes[0].set_xlim([0, 30])

Further considerations

matplotlib has several options for sharing axes in the matplotlib.pyplot.subplots command:

sharex, sharey : bool or {‘none’, ‘all’, ‘row’, ‘col’}, default: False

Controls sharing of properties among x (sharex) or y (sharey) axes:

  • True or ‘all’: x- or y-axis will be shared among all subplots.
  • False or ‘none’: each subplot x- or y-axis will be independent.
  • ‘row’: each subplot row will share an x- or y-axis.
  • ‘col’: each subplot column will share an x- or y-axis.

When subplots have a shared x-axis along a column, only the x tick labels of the bottom subplot are visible. Similarly, when subplots have a shared y-axis along a row, only the y tick labels of the first column subplot are visible.

It would be good to support all of those options from facets via sharex and sharey keyword arguments. (I think the example above would be accomplished through sharex='all' and sharey='all').

@spencerkclark
Copy link
Owner Author

AxesGrid only supports a share_all argument, which one can either set to True or False. It would be nice to enable the other share modes. In order to enable that, we would need a way to toggle axes sharing after Axes objects were created (which is discussed above).

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