【问题标题】:Creating multiple boxplots using plotly使用 plotly 创建多个箱线图
【发布时间】:2020-09-14 19:44:31
【问题描述】:

我正在尝试使用 plotly 复制我在 matplotlib 中制作的以下箱线图:


我的数据在一个从 Excel 文件导入的非常简单的数据框中,如下所示:

如您所见,我希望在 x 轴和 y 轴上有不同的条件,范围从 0 到 10,并且每个条件都有不同的箱线图,具有自己的均值、SD 等。但是,当我尝试以下代码时,我得到一个非常奇怪的输出:

import plotly.express as px
conditions = df1.keys() #all conditions, df1 being my DataFrame
conditions=list(conditions)
fig = px.box(df1, x = conditions) 
fig.show()

输出:

任何人都知道我该怎么做才能获得与我使用 matplotlib 但使用 plotly 获得的类似的情节?

【问题讨论】:

    标签: python python-3.x matplotlib plotly boxplot


    【解决方案1】:

    您可以使用plotly.expressplotly.graph_objects

    读取数据:

    import pandas as pd
    
    df = pd.read_csv(r'Documents\test.csv', sep=',')
    print(df)
    print(df.to_dict())
    
       p=0; t=0  p=1; t=6"  p=1; t=30"  p=3; t=6"  p=3; t=30"  p=3; t=1'
    0         0          3           3          2          10         10
    1         2          3           5          4           9          9
    2         2          6           1          1          10          9
    3         1          1           4          2           7          8
    
    {'p=0; t=0': {0: 0, 1: 2, 2: 2, 3: 1}, 'p=1; t=6"': {0: 3, 1: 3, 2: 6, 3: 1}, 
     'p=1; t=30"': {0: 3, 1: 5, 2: 1, 3: 4}, 'p=3; t=6"': {0: 2, 1: 4, 2: 1, 3: 2}, 
     'p=3; t=30"': {0: 10, 1: 9, 2: 10, 3: 7}, "p=3; t=1'": {0: 10, 1: 9, 2: 9, 3: 8}}
    

    plotly.graph_objects:

    import plotly.graph_objects as go
    
    fig = go.Figure()
    
    for col in df:
      fig.add_trace(go.Box(y=df[col].values, name=df[col].name))
      
    fig.show()
    

    plotly.express:

    import plotly.express as px
    
    fig = px.box(pd.melt(df), x="variable", y="value", points="outliers")
    fig.show()
    

    【讨论】:

      【解决方案2】:
      import plotly.express as px
      
      df = px.data.tips()
      
      fig = px.box(df, x="day", y="total_bill", color="smoker")
      fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
      fig.show()
      

      【讨论】:

        猜你喜欢
        • 2022-09-24
        • 1970-01-01
        • 2021-07-07
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        • 2022-01-22
        • 2019-03-22
        相关资源
        最近更新 更多