簡體   English   中英

如何使用 Plotly Express 創建子圖

[英]How To Create Subplots Using Plotly Express

如果您像我一樣,您喜歡 Plotly Express,但是當您遇到 Express 返回的數字無法使用“make_subplots()”的問題時,您會感到沮喪,因為 make_subplots 需要跟蹤而不是數字。 在這篇文章中,我想分享我自己的解決方案,了解如何僅使用 Plotly Express(和 plotly.subplots)創建包含兩種不同類型圖形(如下所示)的子圖

在此處輸入圖像描述

解決方案:

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)

由於我正在處理的項目數據的敏感性,我無法分享我的程序 output 的實際圖像,但它看起來與上圖中的完全一樣。 據我測試,這應該適用於任何 Express 人物。

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

公認的答案很好,我想提一下針對它的第一條評論的一個小修改,這只是為了顯示一些特定子圖的傳說。 當所有子圖共享相同的圖例時,我發現此修改非常有用,並且讓單個圖例重復多次似乎是多余的。

這是訣竅。 圖形的每個軌跡都有自己的'showlegend'屬性(Plotly Express 圖形的默認設置為 True),因此您可以通過迭代將其設置為 False,就像我對圖 2 所做的那樣。

# I largely keep the codes and comments the same as the original answer, with the modification highlighted under '#######'
my_df = px.data.medals_long()

# Create figures in Express
figure1 = px.bar(my_df, x = "nation", y = "count", color = "medal")
figure2 = px.line(my_df, x = "nation", y = "count", color = "medal")

# 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 its 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"])):
    ############ The major modification. Manually set 'showlegend' attribute to False. ############
    figure2["data"][trace]['showlegend'] = False             
    figure2_traces.append(figure2["data"][trace])
    
# Create a 1x2 subplot
this_figure = make_subplots(rows = 1, cols = 2, subplot_titles = ['Bar', 'Line'])
this_figure.update_layout(height = 500, width = 1200, title_text = "Medals count by country", title_font_size = 25)

# Get the Express fig broken down as traces and add the traces to the proper plot within 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)
    
this_figure.show()

在此處輸入圖像描述

如果不進行此修改,每個組的圖例將顯示兩次,但它們仍然只能同時顯示/隱藏。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM