简体   繁体   中英

Merge two plots into a single figure with two subplots in plotly

I have two plots made with 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,
)

I am looking for something similar to fig = go.Figure(data = current_plot.data+voltage_plot.data) but with the following result:

在此处输入图像描述

Is this possible?

If you perform the necessary data operations, which should not be hard in this case, you can make a facet/trellis plot with plotly express instead of using go.Figure() and make_subplots . This way, the merging will take place in the dataset, and your call to plotly express will look like this:

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

Plot

在此处输入图像描述

Complete code:

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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