Plotly: Plot multiple figures as subplots

前端 未结 4 1378
既然无缘
既然无缘 2020-12-24 08:22

These resources show how to take data from a single Pandas DataFrame and plot different columns subplots on a Plotly graph. I\'m interested in creating figures from separate

4条回答
  •  情书的邮戳
    2020-12-24 09:25

    Here's a short function in a working example to save a list of figures all to a single HTML file.

    def figures_to_html(figs, filename="dashboard.html"):
        dashboard = open(filename, 'w')
        dashboard.write("" + "\n")
        for fig in figs:
            inner_html = fig.to_html().split('')[1].split('')[0]
            dashboard.write(inner_html)
        dashboard.write("" + "\n")
    
    
    # Example figures
    import plotly.express as px
    gapminder = px.data.gapminder().query("country=='Canada'")
    fig1 = px.line(gapminder, x="year", y="lifeExp", title='Life expectancy in Canada')
    gapminder = px.data.gapminder().query("continent=='Oceania'")
    fig2 = px.line(gapminder, x="year", y="lifeExp", color='country')
    gapminder = px.data.gapminder().query("continent != 'Asia'")
    fig3 = px.line(gapminder, x="year", y="lifeExp", color="continent",
                   line_group="country", hover_name="country")
    
    figures_to_html([fig1, fig2, fig3])
    

提交回复
热议问题