John Smith
John Smith

Reputation: 525

How To Combine Chart And Table Into One Plotly Image

I have successfully created a plotly bar chart image with the following snippet of code:

chart = go.Figure(data=chart_matrix)

And have successfully created a second plotly table image with the following snippet of code:

trace_table = go.Table(
    type='table',
    header=dict(values=table_title),
    cells=dict(values=sorted_matrix)
)

Now I want to combine the 2 images into into 1 (ie have the bar chart with table underneath) but I cannot seem to be able to achieve this. I've tried using subplots but the plotly documentation says that there is no native way to insert a Plotly Table into a Subplot. Other documentation specifies using layouts combined with axes and domains which I don't understand.

Can someone share a simple example of how to do this please? Thanks

Upvotes: 4

Views: 11425

Answers (2)

kkashyap1707
kkashyap1707

Reputation: 531

import plotly.graph_objs as go

trace = go.Table(
    header=dict(values=['A Scores', 'B Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),
    domain=dict(x=[0, 1],
                y=[0, 0.3])
)

trace1 = go.Table(
    header=dict(values=['C Scores', 'D Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),

)


layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))



fig = go.Figure(data = [trace,trace1], layout = layout)
fig.show()

Upvotes: 0

Naren Murali
Naren Murali

Reputation: 56650

On going through the tutorial for table subplots, I was able to replicate what you needed!

Sadly, we can't use Subplots as you said, but this other method is not that complicated, I will try to explain it to the best of my ability, so that you can implement it for other use cases.

You need to know of the property domain, where we can specify a dictionary containing the x and y start and end values by doing so we can position the graphs in different places and this concept is what's being used to create the subplots.

Note: The domain x and y zero values start from the bottom left of the canvas.

So for the table we can see the domain is as below.

domain=dict(x=[0, 1], y=[0, 0.3])

As you can see we say that it should start from the left end and stretch to the right end of the plotly canvas, but if you look at the y values, we say that it should start from the bottom and stretch only to 0.3 (30%) of the plotly canvas, thus there is room to accomodate the bar chart also!

As for the bar chart the domain is specified using a different method, the problem is we do not have a convenient domain parameter, thus we can position it using, its x and y axis.

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[4, 5, 6],
    xaxis="X1",
    yaxis="Y1"
)

layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))

Here we can see that, in the bar chart definition, we just need to set the names of the x and y axis of the chart and in the layout we can define the domain similar to what we have done in the table.

Please let me know if you face any issues implemented and if your issue is resolved completely!

Below is a working snippet for you to refer!

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
from plotly import tools
import pandas as pd
import numpy as np
from datetime import datetime
init_notebook_mode(connected=True)

trace = go.Table(
    header=dict(values=['A Scores', 'B Scores']),
    cells=dict(values=[[100, 90, 80, 90],
                       [95, 85, 75, 95]]),
    domain=dict(x=[0, 1],
                y=[0, 0.3]))

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[4, 5, 6],
    xaxis="X1",
    yaxis="Y1"
)

layout = dict(xaxis1=dict( dict(domain=[0, 1], anchor='y1')),
             yaxis1=dict( dict(domain=[0.38, 1], anchor='x1')))
fig = go.Figure(data = [trace,trace1], layout = layout)
iplot(fig, filename = 'basic_table')

Upvotes: 5

Related Questions