【问题标题】:How To Create Subplots Using Plotly Express如何使用 Plotly Express 创建子图
【发布时间】:2021-07-21 06:36:56
【问题描述】:

如果你和我一样,你喜欢 Plotly Express,但是当你遇到 Express 返回的图形无法使用“make_subplots()”的问题时,你感到很沮丧,因为 make_subplots 接收的是跟踪而不是图形。在这篇文章中,我想分享我自己的解决方案,即我如何仅使用 Plotly Express(和 plotly.subplots)创建一个包含两种不同类型图形(如下所示)的子图

【问题讨论】:

    标签: python plotly plotly-dash plotly-python plotly-express


    【解决方案1】:

    解决方案:

    import plotly.express as px
    import plotly.subplots as sp
    
    # Create figures in Express
    figure1 = px.line(my_df)
    figure2 = px.bar(my_df)
    
    # For as many traces that exist per Express figure, get the traces from each plot and store them in an array.
    # This is essentially breaking down the Express fig into it's traces
    figure1_traces = []
    figure2_traces = []
    for trace in range(len(figure1["data"])):
        figure1_traces.append(figure1["data"][trace])
    for trace in range(len(figure2["data"])):
        figure2_traces.append(figure2["data"][trace])
    
    #Create a 1x2 subplot
    this_figure = sp.make_subplots(rows=1, cols=2) 
    
    # Get the Express fig broken down as traces and add the traces to the proper plot within in the subplot
    for traces in figure1_traces:
        this_figure.append_trace(traces, row=1, col=1)
    for traces in figure2_traces:
        this_figure.append_trace(traces, row=1, col=2)
    
    #the subplot as shown in the above image
    final_graph = dcc.Graph(figure=this_figure)
    

    由于我正在处理的项目数据的敏感性,我无法分享我的程序输出的实际图像,但它看起来与上图中的完全一样。据我测试,这应该适用于任何 Express 人物。

    我希望这对某些人有用。祝大家策划愉快!

    【讨论】:

    • 是否可以只显示一张图的图例?我应该做出哪些改变? (只显示 trace0 而不是 trace1)
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 2020-10-20
    • 2021-10-25
    • 2021-01-23
    • 1970-01-01
    • 2019-11-04
    • 2021-01-12
    • 2022-01-10
    相关资源
    最近更新 更多