Is there anyway to merge multiple exported plot in one with plotly?

Viewed 44

I saved several png, html and json types of graphs, by using python code typed below.

fig = px.scatter(df_new, x="x_label", y="y_label", color='color', text='criteria', title='title_name',
             color_discrete_map={'black': '#000000', 'red': '#FF0000', 'blue': '#4000FF',
                                 'orange': '#FFB200', 'purple': '#BD00FF', 'green': '#00B436'})
fig.update_traces(textposition='bottom center')
fig.add_vline(x=price_avg, line_width=3, line_dash="dash", line_color="black")
fig.add_vrect(x0=start_value, x1=end_value, line_width=0, fillcolor="red", opacity=0.3)

fig.write_json(f'plotly\filename.json')
pio.write_image(fig, f'plotly\filename.png', width=1920, height=1080, scale=4)
fig.write_html(f'plotly\filename.html')

and I successfully reloaded the plot data with this code.

fig = plotly.io.read_json(f'plotly\filename.json')

but I can't turn these 4 figs toward subplots and make one plot by using code below

fig_total = make_subplots(rows = 2, cols = 2, start_cell= 'top-left')
for i in range(0, 4):
    fig = plotly.io.read_json(f'plotly\filename.json')
    fig_total.add_trace(fig, row = int(i / 2) + 1, col = (i % 2) + 1) # << error detected

The error was

ValueError:
    Invalid element(s) received for the 'data' property of
        Invalid elements include: [Figure({
    'data': [{'hovertemplate': 'color=orange<br>...*content*

The 'data' property is a tuple of trace instances
that may be specified as:
  - A list or tuple of trace instances
    (e.g. [Scatter(...), Bar(...)])
  - A single trace instance
    (e.g. Scatter(...), Bar(...), etc.)
  - A list or tuple of dicts of string/value properties where:
    - The 'type' property specifies the trace type
        One of: ['bar', 'barpolar', 'box', 'candlestick', ...plot types...

Is there any solution that turning these figs as subplot and merging into one plot or do I have to disintegrate json file to suitable form, viable in add_trace()?

0 Answers
Related