Creating a subplot from 2 exisitng plots

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px

fig1 = px.area(CPI_and_Interest, x=CPI_and_Interest.index, y=Chi_so)

fig2 = px.line(CPI_and_Interest, x=CPI_and_Interest.index, y=[‘Overnight’])

fig = make_subplots(rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.02)

fig.add_trace(fig1, row=1, col=1)
fig.add_trace(fig2, row=2, col=1)

fig.show()

I have been trying to create a subplot with these 2 above plots (fig1 and fig2).
However, it keeps showing me this following error:

  • All remaining properties are passed to the constructor of the specified trace type

      (e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])
    

Any ideas how to fix this?

@Anthony_Nguyen3006 I think it is asking you for the type of subplot.

If you go to plotly subplots library you will see the following code:

from plotly.subplots import make_subplots
import plotly.graph_objects as go

fig = make_subplots(rows=1, cols=2)

fig.add_trace(
    go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
    row=1, col=1
)

fig.add_trace(
    go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
    row=1, col=2
)

fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()

So, it shows that you need to choose the type of plot whether it is scatter or bar.
I hope you find this useful

Sorry, I didn’t see this line:

fig1 = px.area(CPI_and_Interest, x=CPI_and_Interest.index, y=Chi_so)

fig2 = px.line(CPI_and_Interest, x=CPI_and_Interest.index, y=[‘Overnight’])

You imported plotly.graph_objects as go and you use px, try to change that to go and see if it will work

These 2 plots work fine individually with the use of px.

The problem arises when I try to combine these 2 plots into 1 plot with subplot using make_subplots.

If you want to use subplot, you can’t pass fig1 and fig2 from plotlyexpress, see the codes above to see how you do it when you want to use subplot.

It should now be from graph objects

fig1 =   go.area(CPI_and_Interest, x=CPI_and_Interest.index, y=Chi_so)

fig2 = go.line(CPI_and_Interest, x=CPI_and_Interest.index, y=[‘Overnight’])
1 Like

Hi! @trinityimma is right, to make subplots you need to pass the traces, not the whole figure. However, you can access the traces of a figure created with plotly express by iterating over the fig1.data property. It works both for plots with one trace (fig2 in my example) or with three traces (one per color, as in fig1).

Could you try this in your case?

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px

df = px.data.iris()
fig1 = px.scatter(df, x="sepal_width", y="sepal_length", color='species')
fig2 = px.scatter(df, x="sepal_width", y="petal_width")

fig = make_subplots(
    rows=2, cols=1,
    shared_xaxes=True,
    vertical_spacing=0.02
    )

# add each trace (or traces) to its specific subplot
for i in fig1.data :
    fig.add_trace(i, row=1, col=1)

for i in fig2.data :    
    fig.add_trace(i, row=2, col=1)

fig.show()
1 Like

hey thanks man. This works perfectly.

Just a side question. Do you know how to draw Stacked area using graph objects? It seems that the plotly documentation does not cover this.