小提琴图以图解形式包含各个数据点

han*_*ans 3 python plotly violin-plot

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

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

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()
Run Code Online (Sandbox Code Playgroud)

小提琴情节

小智 5

您可以使用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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述