Plotly 3.5.0's compatibility

hello, guys, recently I upgrade to plotly 3.5.0 in order to use FigureWidegt feature, previous version was 2.4.0. I didn’t go far with 3.5.0 because when i ran my previous codes it gave me a TypeError

paste part of code :

import plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff

subtitles = [‘A’,‘B’]
fig = py.tools.make_subplots(rows=2, cols=1, subplot_titles=subtitles)
trace1 = go.Scatter(x=[1, 2, 3], y=[3, 1, 6])
fig.append_trace(trace1,1,1)

data_matrix = [[‘Country’, ‘Year’, ‘Population’],
[‘United States’, 2000, 282200000],
[‘Canada’, 2000, 27790000],
[‘United States’, 2005, 295500000],
[‘Canada’, 2005, 32310000],
[‘United States’, 2010, 309000000],
[‘Canada’, 2010, 34000000]]

table = ff.create_table(data_matrix)
table_trace = table[‘data’][0]
fig.append_trace(table_trace, 2, 1)

fig[‘layout’][‘xaxis2’] = dict(fig[‘layout’][‘xaxis2’], **table[‘layout’][‘xaxis’])
fig[‘layout’][‘yaxis2’] = dict(fig[‘layout’][‘yaxis2’], **table[‘layout’][‘yaxis’])

it was ok under plotly 2.4, but after I upgraded, it goes with a typeError:

fig[‘layout’][‘xaxis2’] = dict(fig[‘layout’][‘xaxis2’], **table[‘layout’][‘xaxis’])
TypeError: type object argument after ** must be a mapping, not XAxis

Please advise T.T
my environment is python3.6 with Pycharm on Mac

Hi @jin,

I’d recommend reading through the version 3 migration guide (https://github.com/plotly/plotly.py/blob/master/migration-guide.md) to better understand the changes in version 3. The problem you’re running into is that graph_objs objects are no longer subclasses of the Python dict class and the ** expansion operator doesn’t work. You can convert graph_objs instances to dicts using the to_plotly_json() method.

So you should be able to do something like

fig['layout']['xaxis2'] = dict(fig['layout']['xaxis2'].to_plotly_json(), **table['layout']['xaxis'].to_plotly_json())

Or you can restructure your code to avoid the ** operation on graph_objs.

Hope that helps!
-Jon

Thank you , it did help!!

1 Like