1

I want to keep the figures in the exact same way, for example, having different data source, subplot title and respective legends near EACH figure.

currently the code is sth. like

fig1 = px.line(df_crude_spot_long, x="Date", y="$/bbl", color='type', title='Benchmark Crude Spot Prices', color_discrete_sequence=px.colors.qualitative.Bold)
fig1.update_layout(
    xaxis_title="",
    
    title={
        # 'text': "Plot Title",
        # 'y':0.9,
        'x':0.5},
    
    legend_title="Benchmark",
    font=dict(
        family="Courier New, monospace",
        size=40,
        color="navy"),
    

    legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.01
)
)

fig2 = px.line(df_crude_futures, x="contract month", y='Price', color='futures', title='Latest Crude Oil Futures', color_discrete_sequence=px.colors.qualitative.Bold)
fig2.update_layout(
    
    title={
        # 'text': "Plot Title",
        # 'y':0.9,
        'x':0.5},
    
    legend_title="Futures",
    font=dict(
        family="Courier New, monospace",
        size=40,
        color="navy"),
    

    legend=dict(
    yanchor="top",
    y=0.99,
    xanchor="left",
    x=0.8
)
)

fig3
fig4
....

As you can see, the data source for different figures are not from the same dataframe.

I tried the 2nd approach in this post, by combining make_subplots and plotly express with code like this

Is it possible to create a subplot with Plotly Express?

fig = make_subplots(
    rows=2, cols=2,
    subplot_titles=("BenchmarkPrices", "Latest Oil Futures", "Bunker Prices", "Fuel Futures"))

for d in fig1.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'], name = d['name'])), row=1, col=1)

for d in fig2.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'],  name = d['name'])), row=2, col=1)
    

for d in fig3.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'], name = d['name'])), row=2, col=1)

for d in fig4.data:
    fig.add_trace((go.Scatter(x=d['x'], y=d['y'],  name = d['name'])), row=2, col=2)

but the result has some of the figures not shown properly and all the legends put together on the right side.

enter image description here

I mentioned Html file in the title because I generally save my figure like below

offline.plot({'data':fig},filename='charts.html',auto_open=False)

Update 1 The comment section suggested the method in this post Plotly saving multiple plots into a single html

it puts several figures under 1 html but doesn't solve my problem because 1.I need to produce a 2 x 4 charts(2 charts per row and 4 rows in total), with this way it only put 1 chart per row. 2. when I download png from the html, it only shows the 1st figure, even on the html we see the 4

neutralname
  • 383
  • 2
  • 4
  • 11
  • Do you have a current output diagram so I can see what the assignment looks like? Do you have sample data that can be reproduced? – r-beginners Oct 11 '22 at 13:15
  • Thanks for trying to help. For data you can use any data as my question is general: I was just trying to say that they are 4 different dataframes for the 4 charts. I just uploaded the current output, the circled parts are what is not properly shown compared to those individual figure – neutralname Oct 11 '22 at 13:22
  • In which order do you want to save the 4 figures to HTML file? – Hamzah Oct 11 '22 at 13:29
  • 1
    Does this answer your question https://stackoverflow.com/a/59869358/16733101? – Hamzah Oct 11 '22 at 13:46
  • thanks Hamzah, I tried with the method of that post but nothing happens, it sorts of work but I need to produce a 2 x 4 charts(2 charts per row and 4 rows in total), with this way it only put 1 chart per row. – neutralname Oct 11 '22 at 14:04
  • also when I download png from the html, it only shows the 1st figure, even on the html we see the 4 – neutralname Oct 11 '22 at 14:20
  • 1
    There is no option to create a legend for each plot, look at this link https://community.plotly.com/t/associating-subplots-legends-with-each-subplot-and-formatting-subplot-titles/33786/2 – Hamzah Oct 11 '22 at 15:00
  • Thanks Hamzah, I agree, saw this post maybe a year ago. I still have hope as my question is different, it's about put different figure objects into one html file and one png/jpg photo – neutralname Oct 12 '22 at 08:57

0 Answers0