I have 10 traces in a plot and another 10 in another plot. Both have same trace names, but the data is different, i would like to have them in a single html file using sub-plot. How can i combine them, any help would be appreciated.
Hi @rasika,
You have to add those traces to your figure. The only inconvenient is that having all the same name the legend
inserts that name 20 times:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=10,
cols=2,
#print_grid=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
subplot_titles = [f'Title_{i+1}_{j+1}' for i in range(10) for j in range(2)]
)
trace1 = go.Bar(x=['A', 'B', 'C', 'D', 'E', 'F', 'G'],
y=[ 7, 10, 11, 19, 14, 9, 11],
name='Bars',
marker=dict(color= 'rgba(220,49,72, 0.8)', line= dict(width= 1)))
for i in range(10):
for j in range(2):
fig.add_trace(trace1, i+1, j+1)#
fig.update_layout(height=1200)
To get the legend attributes just run:
help(go.layout.Legend)
You can also set in fig.layout
, showlegend=False
, to prevent displaying the trace name so many times.
Your example does not give me any hint on how to specify row or column number. Can you please elaborate a bit
In these lines of code"
for i in range(10):
for j in range(2):
fig.add_trace(trace1, i+1, j+1)#
i+1
is row, and j+1
is column.
You have only to pass your traces instead of trace1.
ok, thanks for the reply, my source files are different for each plot, can i create and merge 2 fig?
Yes you can but there are a few tricks. Could you give more details, pls? What is len(fig1.data) and len(fig2.data)?
10 and 10?
Yes, both will have 10 traces each
@rasika
i
fig1 = ...
fig2 = ...
fig = make_subplots(rows=10,
cols=2,
#print_grid=True,
horizontal_spacing=0.05,
vertical_spacing=0.05,
subplot_titles = [f'Title_{i+1}_{j+1}' for i in range(10) for j in range(2)]
)
for i in range(10):
fig.add_trace(fig1.data[i], i+1, 1)
fig.add_trace(fig2.data[i], i+1, 2)
I dont understand this part
for i in range(10): fig.add_trace(fig1.data[i], i+1, 1) fig.add_trace(fig2.data[i], i+1, 2)
Why do we need this?
I want to have 20 traces displayed in 2 columns, basically i am generating 2 files, i want file 1 to be displayed on the left and the other one on the right, sorry i i have confused you. Right now, i am getting all traces displayed in their own row and column
And also i want both the plots to display the data as i hover on any one of them
You said you have two figures, each one of 10 traces and want a subplot that has on the first column the traces in the first figure, and on the second column the traces from fig2. The code above implenents something like that.
Iβm sorry but I stop to help you because you donβt give ckear derails and donβt post a use case. On stackoverflow such questions are not answered.
Extremely sorry, i need to display 2 figures in a same html file. these 2 figures has same x axis but y axis values are different and when i hover over the x - axis both of them should display the data. Extremely sorry for the confusion created.