plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.11k stars 2.54k forks source link

Using sankey subplots breaks plots depending on trace order. #1527

Closed SaschaJust closed 5 years ago

SaschaJust commented 5 years ago

Hi,

today I ran into an issue using sankey diagrams in combination with other plots. This worked just fine in the past, but when I reran notebooks today all plots containing sankeys were broken.

Observed Behavior

I dug a little deeper and found out that the order of the traces affects the success of the plot. Having sankey as first entry in the Figure.data list works just fine—otherwise the plot breaks. The sankey is not rendered at all and the coordinates of the other plots are off.

Reproduction

import plotly.offline as py
import plotly.graph_objs as go
from plotly import __version__
py.init_notebook_mode(connected=True)
print(__version__)

sankey = go.Sankey(
    domain = go.sankey.Domain(
        x = [0, .45]
    ),
    node=go.sankey.Node(
        label = ['A', 'B', 'C', 'D']
    ),
    link=go.sankey.Link(
        source = [0, 0, 1, 1],
        target = [2, 3, 2, 3],
        value =  [1, 1, 1, 1]
    ),
)

scatter = go.Scatter(
    xaxis='x2'
)

layout = go.Layout(
    xaxis2 = go.layout.XAxis(
        domain = [.55, 1]
    ),
    annotations = [go.layout.Annotation(
        xref='x2',
        x = 1,
        y = 1,
        text = '(1,1)'
    )]
)

Then plotting it like this will work:

fig = go.Figure(data = [sankey, scatter], layout = layout)
py.iplot(fig)

sankey_subplot_working

Using a different order will fail to render the sankey and throw of the coordinates of the scatter plot.

fig = go.Figure(data = [scatter, sankey], layout = layout)
py.iplot(fig)

sankey_subplot_broken

I'm assuming this is a bug, as this has worked before just fine.

I'm not sure whether this is an issue with plotly.py or plotly.js. Apologies in advance, in case this is the wrong tracker.

jonmmease commented 5 years ago

Thanks a lot for the report, and especially for the helpful example. I've opened https://github.com/plotly/plotly.js/issues/3798 to track this in the JavaScript library.

jonmmease commented 5 years ago

Closed by https://github.com/plotly/plotly.js/pull/3802 and will be released in plotly.py 3.9.0

jonmmease commented 5 years ago

Hi @SaschaJust, could you try out the 3.9.0 release candidate and check whether this takes care of the issue for you? installation instructions at https://github.com/plotly/plotly.py/blob/release_3.9.0/README.md#installation.

Thanks!

SaschaJust commented 5 years ago

@jonmmease, this now works perfectly fine in plotly.js and plotly.py (tested with 3.9.0rc1).

Side note: upgrading to 3.9.0 is not required as there were no changes required in the python library and plotly.py loads plotly.latest.min.js, anyways.

Thanks for the great support!