在 spyder 中使用 seaborn 根据列中的值间隔 x 轴

Spacing x-axis based on values in a column using seaborn in spyder

我正在使用 seaborn 生成包含 5 个不同条件及其对应值的箱线图。我希望 x 轴值根据它们的实际值以彼此正确的距离绘制,而不是均匀间隔的箱线图。我有一个大数据集,但为了简化我的问题,我用一个较小的数据集编造了一些数字。 我有以下代码:

dff=pd.DataFrame()
dff['Concentration']=[150,150,150,150,300,300,300,300,525,525,525,525,600,600,600,600,650,650,650,650]
dff['Counts']=[0.677,0.703,0.800,0.705,1.0,0.906,1.11,0.877,1.255,1.38,1.25,1.38,1.4,1.5,1.6,1.667,1.65,1.66,1.70,1.599]

figxyz,ax=plt.subplots(1,1,figsize=(4,4))
sns.boxplot(dff['Concentration'],dff['Counts'],color='tan')
sns.swarmplot(dff['Concentration'],dff['Counts'],color='black')

我得到以下 boxplot。我希望浓度之间的距离对应于实际数字之间的距离。例如,150 和 300 箱线图之间的距离应小于 300 和 525 之间的距离,最小距离应在 600 和 650 之间。

我也尝试使用以下代码,它似乎移动了 x 刻度,但将箱线图一直聚集在左侧,如 this。

ax.set_xticks(dff['Concentration']) 

您可能需要使用 matplotlib 的 boxplot() 而不是 seaborn 的:

data = {k:v.values for k,v in dff.groupby('Concentration')['Counts']}

fig, ax = plt.subplots()
ax.boxplot(x=data.values(), positions=list(data.keys()), widths=50, sym='')
ax.plot(dff['Concentration'],dff['Counts'],'o')