Seaborn boxplot水平线批注

2024-03-29 08:48:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一些绘图中添加一条水平线,即“目标”线:条纹图、方框图和小提琴图,以显示理想值数据(或理想的范围)。在

这个R示例(Add multiple horizontal lines in a boxplot)-第一个图像-基本上就是它(尽管我要做一些格式化以使其更具代表性)。在

R abline() equivalent in Python对我没有帮助(或者我还没弄明白怎么做),因为我使用的是分类数据,所以我只想从本质上定义(例如)y=3并将其绘制出来。 我的代码(下面)运行良好,我只是不知道如何添加一行。在

fig, ax = plt.subplots(nrows=4,figsize=(20,20))

sns.violinplot(x="Wafer", y="Means", hue='Feature', 
           data=Means[Means.Target == 1], ax=ax[0])
sns.violinplot(x="Wafer", y="Means", hue='Feature', 
           data=Means[Means.Target == 3], ax=ax[1])
sns.boxplot(x="Feature", y="Means", 
        data=Means, linewidth=0.8, ax=ax[2])
sns.stripplot(x="Feature", y="Means", hue='Wafer',
          data=Means, palette="plasma", jitter=0.1, size=5.5, ax=ax[3])

非常感谢任何帮助。在


Tags: 数据in绘图targetdataaxhuefeature
2条回答

假设您不需要绘制一条高度为y且从x1到{}的水平线,其中x1和{}是实际的x数据值。以下是几种可能的方法中的三种:

第一个:

ax.hlines(y, x1, x2)

第二:

^{pr2}$

第三个(x1x2现在在0表示最左边,1表示最右边)之间的相对/分数坐标:

ax.axhline(y, x1, x2)

如果你想定义一个好的或坏的区域,我通常会发现在数据后面放一个补丁更容易让用户理解。在

fig, ax = plt.subplots(figsize=(4, 4))
ax.plot([1,2,3,4], [1,2,3,4], color='blue')  # simple example line

# define patch area
rect = patches.Rectangle(
    xy=(ax.get_xlim()[0], 2),  # lower left corner of box: beginning of x-axis range & y coord)
    width=ax.get_xlim()[1]-ax.get_xlim()[0],  # width from x-axis range
    height=1,
    color='green', alpha=0.1, ec='red'
)
ax.add_patch(rect)
plt.show()

enter image description here

相关问题 更多 >