Python Seaborn箱线图水平线注释
我想在一些图中添加一条水平线,即“目标”线:条纹图、盒形图和小提琴图,以显示理想值数据(或理想范围) 这个R示例()-第一个图像-基本上就是它(尽管我会做一些格式化以使其具有可展示性) 在使用分类数据时,这对我没有帮助(或者我还没有弄清楚如何做),所以我只想从本质上定义(例如,Python Seaborn箱线图水平线注释,python,pandas,seaborn,boxplot,Python,Pandas,Seaborn,Boxplot,我想在一些图中添加一条水平线,即“目标”线:条纹图、盒形图和小提琴图,以显示理想值数据(或理想范围) 这个R示例()-第一个图像-基本上就是它(尽管我会做一些格式化以使其具有可展示性) 在使用分类数据时,这对我没有帮助(或者我还没有弄清楚如何做),所以我只想从本质上定义(例如,y=3)并绘制它。 我的代码(如下)运行良好,我只是不知道如何添加行 fig, ax = plt.subplots(nrows=4,figsize=(20,20)) sns.violinplot(x="Wafer", y
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])
非常感谢您的帮助。假设您不想在高度
y
和从x1
到x2
绘制一条水平线,其中x1
和x2
是您的实际x数据值。以下是几种可能的方法中的三种:
第一:
ax.hlines(y, x1, x2)
第二:
plt.plot([x1, x2], [y, y])
第三(x1
和x2
现在处于0
表示最左侧和1
表示最右侧之间的相对/分数坐标中):
如果您想定义一个好的或坏的区域,我通常会发现在数据后面放置一个补丁更容易让用户解释
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()
应该是
plt.hlines(y,xmin,xmax)
谢谢-你的第三个版本正是我想要的。如何对多行执行此操作:例如,ax.axhline[(3,0,1),(1,0,1)]
(这不起作用)如果我想在y=3
和y=1
处执行一行,那么您只需编写两行:ax.axhline(1,0,1)
和ax.axhline(3,0,1)
。你也可以把它放在一个for
循环中,但是对于两行,我认为没有必要,我实际上是在for循环中这样做的,因为我无法让它工作(在上面代码的上下文中),调用函数两次。for循环工作得很好,谢谢。谢谢你,这是一个很好的观点;我将尝试实施和反馈。
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()