Как создать подзаголовки с помощью Plotly Express

Если вы похожи на меня, вы любите Plotly Express, но были разочарованы, когда столкнулись с проблемой, что данные, возвращаемые Express, не могут использовать make_subplots (), поскольку make_subplots принимает следы, а не цифры. В этом посте я хотел бы поделиться своим собственным решением о том, как я создал подзаголовок, содержащий два разных типа фигур (как показано ниже), используя только Plotly Express (и plotly.subplots)

введите описание изображения здесь


person mmarion    schedule 27.04.2021    source источник


Ответы (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.

Я надеюсь, что это может быть полезно для некоторых. Всем удачи!

person mmarion    schedule 27.04.2021