我正在尝试使用plotly在单个图中创建多个表,并想知道如何做到这一点。
我有一个像这样的数据集:
data_US = [['Country', 'Year', 'Population'],
['United States', 2000, 282200000],
['United States', 2005, 295500000],
['United States', 2010, 309000000]
]
data_Canada= [['Country', 'Year', 'Population'],
['Canada', 2000, 27790000],
['Canada', 2005, 32310000],
['Canada', 2010, 34000000]]
如何在单个图中显示这些数据集?
这是我的尝试:
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
from plotly import tools
from plotly.offline import plot, iplot, init_notebook_mode
init_notebook_mode(connected=False)
def create_two_tables(lst1,lst2):
table1 = ff.create_table(lst1)
table2 = ff.create_table(lst2)
fig = tools.make_subplots(rows=2,
cols=1,
print_grid=True,
vertical_spacing=0.085,
subplot_titles=('US population', 'Canada population')
)
fig.append_trace(table1['data'][0], 1, 1)
fig.append_trace(table2['data'][0], 2, 1)
fig['layout']['xaxis1']= dict(fig['layout']['xaxis1'].to_plotly_json(),
**table1['layout']['xaxis'].to_plotly_json())
fig['layout']['yaxis1']= dict(fig['layout']['yaxis1'].to_plotly_json(),
**table1['layout']['yaxis'].to_plotly_json())
fig['layout']['xaxis2']= dict(fig['layout']['xaxis2'].to_plotly_json(),
**table2['layout']['xaxis'].to_plotly_json())
fig['layout']['yaxis2']= dict(fig['layout']['yaxis2'].to_plotly_json(),
**table2['layout']['yaxis'].to_plotly_json())
for k in range(len(table2['layout']['annotations'])):
table2['layout']['annotations'][k].update(xref='x2', yref='y2')
fig['layout']['annotations'].extend(
table1['layout']['annotations']+table2['layout']['annotations'])
fig['layout'].update(width=800, height=600, margin=dict(t=100, l=50, r=50, b=50))
iplot(fig, filename='two_tables.html')
# Create two data and make a table
data_US = [['Country', 'Year', 'Population'],
['United States', 2000, 282200000],
['United States', 2005, 295500000],
['United States', 2010, 309000000]
]
data_Canada= [['Country', 'Year', 'Population'],
['Canada', 2000, 27790000],
['Canada', 2005, 32310000],
['Canada', 2010, 34000000]]
create_two_tables(data_US,data_Canada)
但是我收到TYPE ERROR。
该代码在plotly更新之前有效(theory在以上四行中没有.to_plotly_json()
),并且由于API的某些更改,该代码现在无法正常工作。
如何修复代码?