使用带状图在seaborn中用中线绘制点 [英] Drawing points with with median lines in seaborn using stripplot

查看:29
本文介绍了使用带状图在seaborn中用中线绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在seaborn中有以下情节:

I have the following plot in seaborn:

df = pandas.DataFrame({"sample": ["X", "X", "X", "Y", "Y", "Y"],
                       "value": [0.2, 0.3, 0.4, 0.7, 0.75, 0.8],
                       "rep": ["a", "b", "c", "a", "b", "c"]})
plt.figure()
ax = sns.stripplot(x="sample", y="value", edgecolor="none",
                   hue="sample", palette="Set1", data=df)

# how to plot median line?
plt.show()

它以灰阶颜色绘制点,而不使用 Set1 ,并且仅在图例中显示 X ,而不显示 Y :

It plots the points in gray scale colors instead of using Set1 and only shows X in the legend and not Y:

我还想在中间为 X Y 添加一条水平线.如何才能做到这一点? factorplot 似乎没有水平线选项.

I also want to add a horizontal line at the median for X and Y. how can this be done? factorplot doesn't appear to have a horizontal line option.

推荐答案

您可以使用 matplolib 绘制线条.熊猫可能会为您的数据集计算中位数.我在这个例子中使用了 seaborn 0.7.0:

You may plot lines by using matplolib. Pandas may calculate medians value for your dataset. I use seaborn 0.7.0 in this example:

from pandas import DataFrame
import matplotlib.pyplot as plt
import seaborn as sns

df = DataFrame({"sample": ["X", "X", "X", "Y", "Y", "Y"],
                       "value": [0.2, 0.3, 0.4, 0.7, 0.75, 0.8],
                       "rep": ["a", "b", "c", "a", "b", "c"]})
# calc medians
xmed = df.loc[df["sample"] == 'X'].median()['value']
ymed = df.loc[df["sample"] == 'Y'].median()['value']

sns.stripplot(x="sample", y="value", edgecolor="none",
 hue="sample", palette="Set1", data=df)

x = plt.gca().axes.get_xlim()

# how to plot median line?
plt.plot(x, len(x) * [xmed], sns.xkcd_rgb["pale red"])
plt.plot(x, len(x) * [ymed], sns.xkcd_rgb["denim blue"])
plt.show()

这篇关于使用带状图在seaborn中用中线绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆