【问题标题】:Violin plot in plotly enclosing individual data points小提琴图在情节上封闭各个数据点
【发布时间】:2021-06-01 06:21:52
【问题描述】:

如何创建包含单个数据点的小提琴图?

下面的代码显示小提琴和单个数据点并排显示;但是,我希望小提琴将这些点包围起来而不是彼此相邻(洋红色箭头)?

import plotly.express as px

df = px.data.tips()
fig = px.violin(df, y="total_bill", box=False, # draw box plot inside the violin
                points='all', # can be 'outliers', or False
               )
fig.show()

【问题讨论】:

    标签: python plotly violin-plot


    【解决方案1】:

    您可以使用 plotly graph_objects 实现此目的。您必须设置pointpos 属性,该属性设置采样点相对于小提琴的位置。如果为“0”,则采样点位于小提琴中心上方。 (https://plotly.com/python/reference/violin/)

    import plotly.express as px
    import plotly.graph_objects as go
    
    df = px.data.tips()
    
    ### define the chart
    data = go.Violin(y=df['total_bill'], points='all', pointpos=0)
    
    ### assign the chart to the figure
    fig = go.Figure(data=data)
    
    ### show the plot
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2018-05-15
      • 2018-05-15
      • 1970-01-01
      相关资源
      最近更新 更多