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

Secondary y-axis subplot support #1564

Merged
merged 18 commits into from
May 25, 2019
Merged

Secondary y-axis subplot support #1564

merged 18 commits into from
May 25, 2019

Conversation

jonmmease
Copy link
Contributor

@jonmmease jonmmease commented May 13, 2019

Overview

This PR introduces secondary y-axis support to the subplots.make_subplots and associated functions. This functionality is only available in the v4_subplots future mode.

make_subplots update

The subplots.make_subplots function not supports a secondary_y key in the elements of the specs argument. The figure methods for adding traces now also support a secondary_y argument.

For example:

from _plotly_future_ import v4_subplots, renderer_defaults
from plotly import subplots
import plotly.graph_objs as go
fig = subplots.make_subplots(
    rows=1,
    cols=2,
    specs=[[{'secondary_y': True},
            {'secondary_y': True}]]
)

fig.add_bar(y=[1, 3, 2], name='First',
            row=1, col=1);
fig.add_scatter(y=[10, 30, 20], name='Second',
                row=1, col=1, secondary_y=True)

fig.add_bar(y=[2, 1, 3], name='Third',
            row=1, col=2)
fig.add_trace(
    go.Scatter(y=[2, 1, 3], name='Forth'),
    row=1, col=2, secondary_y=True)

fig

newplot-23

get_subplot

fig.get_subplot(1, 1).yaxis.update(title_text='Y-axis 0')
fig.get_subplot(1, 1, secondary_y=True).yaxis.update(title_text='Y-axis 1')
fig.get_subplot(1, 2).yaxis.update(title_text='Y-axis 2')
fig.get_subplot(1, 2, secondary_y=True).yaxis.update(title_text='Y-axis 3')

newplot-21

Update traces

fig.for_each_trace(
    lambda t: t.update(name=t.name.upper()),
    row=1, col=2, secondary_y=True)

newplot-22

Update yaxis

fig.update_yaxes(title_font_color='red', secondary_y=True)

newplot-24

cc @nicolaskruchten @chriddyp

@jonmmease jonmmease changed the title WIP: Secondary y-axis subplot support Secondary y-axis subplot support May 15, 2019
@jonmmease
Copy link
Contributor Author

To try out this PR:

pip install https://12843-14579099-gh.circle-artifacts.com/0/dist/plotly-3.9.0%2B11.g6ec85ce5.tar.gz

@marcotama
Copy link

marcotama commented May 22, 2019

Hi @jonmmease , I am getting an exception using this feature (I installed as per instruction above in my Conda environment running Python 3.7):

    print(plotly.__version__)
    fig = tools.make_subplots(
        rows=2,
        cols=2,
        print_grid=False,
        specs=[
            [{'secondary_y': True}, {'secondary_y': True}],
            [{'secondary_y': True}, {'secondary_y': True}]
        ]
    )

Outputs

3.9.0+11.g6ec85ce5
  File "/Users/marco/PycharmProjects/plotting/metrics_evolution_over_time/grouped.py", line 169, in ply_plotter
    [{'secondary_y': True}, {'secondary_y': True}]
  File "/Users/marco/anaconda3/envs/my_env/lib/python3.7/site-packages/plotly/tools.py", line 585, in make_subplots
    _check_keys_and_fill('specs', specs, SPEC_defaults)
  File "/Users/marco/anaconda3/envs/my_env/lib/python3.7/site-packages/plotly/tools.py", line 570, in _check_keys_and_fill
    _checks(arg_ii, defaults)
  File "/Users/marco/anaconda3/envs/my_env/lib/python3.7/site-packages/plotly/tools.py", line 563, in _checks
    "argument '{name}'".format(k=k, name=name))
Exception: Invalid key 'secondary_y' in keyword argument 'specs'

Looks like these lines in plotly.tools are missing a default for 'secondary_y':

    # Default spec key-values
    SPEC_defaults = dict(
        is_3d=False,
        colspan=1,
        rowspan=1,
        l=0.0,
        r=0.0,
        b=0.0,
        t=0.0
        # TODO add support for 'w' and 'h'
    )
    _check_keys_and_fill('specs', specs, SPEC_defaults)

Changing the above with a default works around the error.

    # Default spec key-values
    SPEC_defaults = dict(
        is_3d=False,
        colspan=1,
        rowspan=1,
        l=0.0,
        r=0.0,
        b=0.0,
        t=0.0,
        secondary_y=False
        # TODO add support for 'w' and 'h'
    )
    _check_keys_and_fill('specs', specs, SPEC_defaults)

However, it seems that my secondary_y are all ignored, as fig.print_grid() outputs

This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3 ]  [ (2,2) x4,y4 ]

Did I do something wrong in the installation process?

I also tried pip install git+git://github.com/plotly/plotly.py.git@enh_secondary_y and got the same problems.

@jonmmease
Copy link
Contributor Author

Hi @marcotama, thanks for giving it a try.

Do you have the from _plotly_future_ import v4_subplots line above the plotly import? This is needed to "turn on" the new subplots functionality. The version 4 make_subplot function should be in plotly.subplots.make_subplots. If that's not the issue, could you double check that there isn't a second version of plotly.py installed (maybe through conda?)

Thanks!

@marcotama
Copy link

marcotama commented May 23, 2019

Hi @jonmmease , I feel a little stupid! I was missing that import line.
It works perfectly! Thanks!
Is this going to be part of 3.9.0? Will it need the _plotly_future_ line?

@jonmmease
Copy link
Contributor Author

Hi @marcotama, great! Thanks for giving it a try. The current stable version is 3.9.0 so this will go into 3.10.0.

And yes, prior to version 4.0 you'll need this _plotly_future_ line to use this functionality. In version 4 this will become the default behavior. So the secondary_y functionality should not be considered stable until version 4, but help with testing is definitely appreciated so please open bug reports for any trouble you run into while using the _plotly_future_ flags. Thanks!

@jonmmease jonmmease merged commit 0c5665f into master May 25, 2019
@jonmmease jonmmease added this to the v3.10 milestone May 25, 2019
@jonmmease jonmmease added the V4 label May 29, 2019
@nicolaskruchten nicolaskruchten deleted the enh_secondary_y branch June 19, 2020 16: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.

2 participants