簡體   English   中英

將兩個圖合並成一個圖,在 plotly 中有兩個子圖

[英]Merge two plots into a single figure with two subplots in plotly

我有兩個用 plotly 制作的圖:

current_plot = px.line(
    measured_data_df,
    x = 'When',
    y = 'Bias current (A)',
    color = 'device_name',
    markers = True,
)
voltage_plot = px.line(
    measured_data_df,
    x = 'When',
    y = 'Bias voltage (V)',
    color = 'device_name',
    markers = True,
)

我正在尋找類似於fig = go.Figure(data = current_plot.data+voltage_plot.data)但結果如下:

在此處輸入圖像描述

這可能嗎?

如果您執行必要的數據操作,在這種情況下應該不難,您可以使用 plotly express 而不是使用go.Figure()來制作 facet/trellis make_subplots 這樣,合並將在數據集中進行,您對 plotly express 的調用將如下所示:

fig = px.line(df, x = 'when', y = 'value', color = 'variable', facet_row = 'bias', height = 600)

Plot

在此處輸入圖像描述

完整代碼:

# imports
import pandas as pd
import plotly.express as px

# data that should represent your actual data nicely
df = px.data.stocks()
df = df[df.columns[:5]]
df.columns = ['when', 'LTT4', 'LTT8', 'LTT21',  'MS33']
df = pd.melt(df, id_vars = ['when'], value_vars = df.columns[1:])
assign = {'LTT4':'current (A)', 'LTT8':'current (A)',
          'LTT21': 'voltage (V)', 'MS33':'voltage (V)'}
df = df.assign(bias = df['variable'].replace(assign))

# facet plot with px.line
fig = px.line(df, x = 'when', y = 'value', color = 'variable', facet_row = 'bias', height = 600)
fig.show()

暫無
暫無

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

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