小提琴图巧妙地包含各个数据点

Violin plot in plotly enclosing individual data points

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

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

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

您可以使用 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()