简体   繁体   English

如何使用 Plotly Express 创建子图

[英]How To Create Subplots Using Plotly Express

If you're like me, you love Plotly Express, but were frustrated when you ran into the issue that figures returned by Express can't utilize 'make_subplots()' since make_subplots takes in traces and not figures.如果您像我一样,您喜欢 Plotly Express,但是当您遇到 Express 返回的数字无法使用“make_subplots()”的问题时,您会感到沮丧,因为 make_subplots 需要跟踪而不是数字。 With this post, I'd like to share my own solution on how I created a subplot containing two different types of figures (like below) using only Plotly Express (and plotly.subplots)在这篇文章中,我想分享我自己的解决方案,了解如何仅使用 Plotly Express(和 plotly.subplots)创建包含两种不同类型图形(如下所示)的子图

在此处输入图像描述

Solution:解决方案:

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)

I couldn't share an actual image of my program's output due to the sensitivity of the data of the project I'm working on, but it looks exactly like the one in the above image.由于我正在处理的项目数据的敏感性,我无法分享我的程序 output 的实际图像,但它看起来与上图中的完全一样。 As far as I've tested this should work with any Express figure.据我测试,这应该适用于任何 Express 人物。

I hope this can be of use to some.我希望这对某些人有用。 Happy plotting everyone!祝大家策划愉快!

The accepted answer is great, and I would like to mention a small modification addressing the first comment of it, which is only to show legends of some specific subplots.公认的答案很好,我想提一下针对它的第一条评论的一个小修改,这只是为了显示一些特定子图的传说。 I find this modification really useful when all subplots share the very same legends and it seems redundant to let a single legend repeat multiple times.当所有子图共享相同的图例时,我发现此修改非常有用,并且让单个图例重复多次似乎是多余的。

Here is the trick.这是诀窍。 Each trace of a figure has its own 'showlegend' attribute (set to True by default for Plotly Express figures), so you may simply set this to False with an iteration, as I did for figure 2.图形的每个轨迹都有自己的'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()

在此处输入图像描述

Without this modification, the legends will show twice for each group, but they can still only be displayed/hidden simultaneously.如果不进行此修改,每个组的图例将显示两次,但它们仍然只能同时显示/隐藏。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM