Skip to content

Allow more than two twin axes #226

Closed

Description

@bradyrx

Description

When calling ax.twinx() more than once, proplot throws the following error:

RuntimeError: No more than *two* twin axes are allowed.

Steps to reproduce

import proplot as plot
fig, ax = plot.subplots()
axes = [ax, ax.twinx(), ax.twinx()]

Equivalent steps in matplotlib

I'm trying to follow this example to make a floating additional y axis: https://stackoverflow.com/questions/7733693/matplotlib-overlay-plots-with-different-scales

import matplotlib.pyplot as plt
import numpy as np
# To make things reproducible...
np.random.seed(1977)

fig, ax = plt.subplots()

# Twin the x-axis twice to make independent y-axes.
axes = [ax, ax.twinx(), ax.twinx()]

# Make some space on the right side for the extra y-axis.
fig.subplots_adjust(right=0.75)

# Move the last y-axis spine over to the right by 20% of the width of the axes
axes[-1].spines['right'].set_position(('axes', 1.2))

# To make the border of the right-most axis visible, we need to turn the frame
# on. This hides the other plots, however, so we need to turn its fill off.
axes[-1].set_frame_on(True)
axes[-1].patch.set_visible(False)

# And finally we get to plot things...
colors = ('Green', 'Red', 'Blue')
for ax, color in zip(axes, colors):
    data = np.random.random(1) * np.random.random(10)
    ax.plot(data, marker='o', linestyle='none', color=color)
    ax.set_ylabel('%s Thing' % color, color=color)
    ax.tick_params(axis='y', colors=color)
axes[0].set_xlabel('X-axis')

plt.show()

Proplot version

0.6.4

Activity

  1. CatNofishing commented on Sep 24, 2020

    @CatNofishing

    Thanks, this is a really powerful program. I love it. It is better than matplotlib.

  2. lukelbd commented on Jun 30, 2021

    @lukelbd
    Collaborator

    I think this is a duplicate of #51 -- eventually I want to support arbitrarily many "hanging" twins (or I guess triplets, quadruplets, ...) with the spines automatically spaced out from each other in an EdgeStack.

    For now though I make twin[xy] enforce rigid formatting and automatically apply axis sharing. So until #51 is implemented I think I'm going to keep the <=2 twins restriction -- otherwise would have to temporarily rewrite a lot of the twin[xy] code.

  3. lukelbd commented on Jun 30, 2021

    @lukelbd
    Collaborator

    ...actually I changed my mind. Looked at it more closely and my alt[xy] restrictions really weren't necessary (think they originally had to do with making it easier to write the tight layout algorithm, but that algorithm has since been simplified).

    It's now fixed -- you can add as many twin axes as you want, and I no longer force twin axes back into a rigid format at drawtime. Here is an adaptation of your example with proplot:

    fig, ax = plot.subplots()
    
    # Twin the x-axis twice to make independent y-axes.
    axs = [ax, ax.twinx(), ax.twinx()]
    
    # Move the last y-axis spine over to the right by 20% of the width of the axes
    axs[-1].spines['right'].set_position(('axes', 1.2))
    
    # And finally we get to plot things...
    colors = ('Green', 'Red', 'Blue')
    for ax, color in zip(axs, colors):
        data = np.random.random(1) * np.random.random(10)
        ax.plot(data, marker='o', linestyle='none', color=color)
        ax.format(ylabel='%s Thing' % color, ycolor=color)
    axs[0].format(xlabel='X-axis')

    example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions