0

I am trying to create multiple tables in a single plot using plotly and wondering how to do it.

I have a dataset like this:

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]]

How can we show these datasets in a single plot?

Here is my attempt:

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)

But I am getting TYPE ERROR. This code was working before the update of plotly (the thery was no .to_plotly_json() to above four lines) and due to some API changes the code is not working now.

How to fix the code?

1 Answer 1

0

this might feel odd solution but try to find about API changes or place question here: https://community.plot.ly/ Good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.