如何使用辅助轴线和误差线创建Python Plotly Figure

时间:2019-12-04 12:08:29

标签: python plotly-python

我正在使用Python建立仿真模型,目前正在进行参数和灵敏度检查。为了测试参数的敏感性,我需要在x轴上绘制参数并在y轴上绘制模型的结果。关键是模型的结果度量类型多样(例如数量和计量表)。下面是一个具体示例(来自我的旧模型):

example of what the result should look like

我试图制作一个包含所有这些信息的绘图,但这是问题所在:

下面的代码是我的数据框的设置方式

import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

from plotly.subplots import make_subplots

np_simulation_outcomes={'Measure':['Measure_A']*20 +['Measure_B']*20+['Measure_C']*20+['Measure_D']*20,
                     'Mean_value':np.concatenate((np.random.randint(150,301,size=40), np.random.rand(40))),
                     'err_value':np.random.rand(80),
                     'parameter':['param_A']*80,
                     'Setting':np.tile(np.arange(1, 21, 1),4)}

df_simulation_outcomes=pd.DataFrame(np_simulation_outcomes)


下面的代码是双轴的工作代码

fig = make_subplots(specs=[[{"secondary_y": True}]])

series_x = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Setting']
series_y = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Mean_value']

fig.add_trace(
    go.Scatter(x=series_x.values, 
            y=series_y.values, 
            name="average_number_measure_A"),
)

series_x = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_C']['Setting']
series_y = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_C']['Mean_value']

fig.add_trace(
    go.Scatter(x=series_x.values, 
            y=series_y.values, 
            name="average_number_measure_C"),
        secondary_y=True
)


下面的代码用于带有错误栏的行

series_x = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Setting']
series_y = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Mean_value']
series_err = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['err_value']


fig = go.Figure(data=go.Scatter(
        x=series_x.values,
        y=series_y.values,
        error_y=dict(
            type='data', # value of error bar given in data coordinates
            array=series_err.values,
            visible=True)
    ))
fig.show()

两者结合似乎无效。

fig = make_subplots(specs=[[{"secondary_y": True}]])

series_x = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Setting']
series_y = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['Mean_value']
series_err = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_A']['err_value']

fig.add_trace(
    go.Figure(data=go.Scatter(
        x=series_x.values,
        y=series_y.values,
        error_y=dict(
            type='data', # value of error bar given in data coordinates
            array=series_err.values,
            visible=True)
    ))


)

series_x = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_C']['Setting']
series_y = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_C']['Mean_value']
series_err = df_simulation_outcomes.loc[lambda x :x['Measure']=='Measure_C']['err_value']


fig.add_trace(
    go.Figure(data=go.Scatter(
        x=series_x.values,
        y=series_y.values,
        error_y=dict(
            type='data', # value of error bar given in data coordinates
            array=series_err.values,
            visible=True)
    )),
        secondary_y=True
)

我尝试了各种选择:添加go.Scatter而不是Figure等。

有人可以帮我解决问题吗?最终数字应包含所有必需的元素。

顺便说一句,我很乐意坚持下去,如果不可能的话,请坚持Seaborn。

0 个答案:

没有答案