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

Using update to initialize an empty array property fails #1072

Closed
jonmmease opened this issue Jul 26, 2018 · 1 comment
Closed

Using update to initialize an empty array property fails #1072

jonmmease opened this issue Jul 26, 2018 · 1 comment
Labels
bug something broken
Milestone

Comments

@jonmmease
Copy link
Contributor

From #1050

Looks like there's a little more to this one - I also can't use update for layout.shapes even with Plotly 3.1.0. The following code prints an empty tuple.

import plotly.graph_objs as go
layout = go.Layout()
shapes = [
    # Line Vertical
    {
        'type': 'line',
        'x0': 1,
        'y0': 0,
        'x1': 1,
        'y1': 2,
        'line': {
            'color': 'rgb(55, 128, 191)',
            'width': 3,
        },
    },
    # Line Horizontal
    {
        'type': 'line',
        'x0': 2,
        'y0': 2,
        'x1': 5,
        'y1': 2,
        'line': {
            'color': 'rgb(50, 171, 96)',
            'width': 4,
            'dash': 'dashdot',
        },
    },
    # Line Diagonal
    {
        'type': 'line',
        'x0': 4,
        'y0': 0,
        'x1': 6,
        'y1': 2,
        'line': {
            'color': 'rgb(128, 0, 128)',
            'width': 4,
            'dash': 'dot',
        },
    },
]
layout.update(shapes=shapes)
print(layout.shapes)
@jonmmease
Copy link
Contributor Author

Legacy (version 2.7) behavior:

  1. If the original layout.shapes property is uninitialized, then the shapes variable is used to initialize the property:
from pprint import pprint
import plotly.graph_objs as go

layout = go.Layout()
shapes = [
    # Line Vertical
    {
        'type': 'line',
        'x0': 1,
        'y0': 0,
        'x1': 1,
        'y1': 2,
        'line': {
            'color': 'rgb(55, 128, 191)',
            'width': 3,
        },
    },
    # Line Horizontal
    {
        'type': 'line',
        'x0': 2,
        'y0': 2,
        'x1': 5,
        'y1': 2,
        'line': {
            'color': 'rgb(50, 171, 96)',
            'width': 4,
            'dash': 'dashdot',
        },
    },
    # Line Diagonal
    {
        'type': 'line',
        'x0': 4,
        'y0': 0,
        'x1': 6,
        'y1': 2,
        'line': {
            'color': 'rgb(128, 0, 128)',
            'width': 4,
            'dash': 'dot',
        },
    },
]
layout.update(shapes=shapes)
pprint(layout.shapes)
[{'line': {'color': 'rgb(55, 128, 191)', 'width': 3},
  'type': 'line',
  'x0': 1,
  'x1': 1,
  'y0': 0,
  'y1': 2},
 {'line': {'color': 'rgb(50, 171, 96)', 'dash': 'dashdot', 'width': 4},
  'type': 'line',
  'x0': 2,
  'x1': 5,
  'y0': 2,
  'y1': 2},
 {'line': {'color': 'rgb(128, 0, 128)', 'dash': 'dot', 'width': 4},
  'type': 'line',
  'x0': 4,
  'x1': 6,
  'y0': 0,
  'y1': 2}]
  1. If the original layout.shapes property is initialized to a list of length N, then the properties of the first N elements of the shapes variable are merged in. Elements beyond N are ignored.
layout2 = go.Layout(shapes=[{'line': {'dash': 'dot'}}])
layout2.update(shapes=shapes)
pprint(layout2.shapes)
[{'line': {'color': 'rgb(55, 128, 191)', 'dash': 'dot', 'width': 3},
  'type': 'line',
  'x0': 1,
  'x1': 1,
  'y0': 0,
  'y1': 2}]
  1. If the original layout.shapes property is an empty list, then N is zero and so the entire shapes variable is ignored
layout3 = go.Layout(shapes=[])
layout3.update(shapes=shapes)
pprint(layout3.shapes)
[]

In plotly.py version 3, what used to be case 1 is now falling into case 3. So we just need to check for the difference between an uninitialized shapes property and an empty shapes property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

No branches or pull requests

1 participant