Seaborn:如何向分布图添加垂直线 (sns.distplot) [英] Seaborn: How to add vertical lines to a distribution plot (sns.distplot)

查看:66
本文介绍了Seaborn:如何向分布图添加垂直线 (sns.distplot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

如何将这种设置与垂直线结合起来,以便可以说明这样的阈值:

我知道我可以像这里一样使用 matplotlib 做到这一点 带有标记目标的线的动态直方图子图,但我真的很喜欢seaborn图的简单性,并想知道是否可以更优雅地完成它(是的,我知道seaborn建立在matplotlib之上)

感谢您的任何建议!

解决方案

只需使用

  plt.axvline(2.8,0,0.17)

另一行也一样

在这里,您可以使用诸如 maxx = max(data) 或类似变量之类的变量来代替 0.17 来放置分布的最大值.2.8 是 x 轴上的位置.哦,请记住,y值必须在0到1之间,其中1是图的顶部.您可以相应地重新调整您的值.另一个明显的选择就是

  plt.plot([2.8,2.8],[0,max(data)])

Using the examples from seaborn.pydata.org and the Python DataScience Handbook, I'm able to produce a combined distribution plot with the following snippet:

Code:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# some settings
sns.set_style("darkgrid")

# Create some data
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])

# Combined distributionplot
sns.distplot(data['x'])
sns.distplot(data['y'])

Plot:

How can I combine this setup with vertical lines so that I can illustrate thresholds like this:

I know I can do it with matplotlib like here Dynamic histogram subplots with line to mark target, but I really like the simplicity of seaborn plots and would like to know if it's possible to do it more elegantly (and yes, I know that seaborn builds on top of matplotlib).

Thank you for any suggestions!

解决方案

Just use

plt.axvline(2.8, 0,0.17)

And the same for the other line

Here instead of 0.17 you can put the maxima of your distribution using some variable such as maxx = max(data) or something similar. 2.8 is the position on the x-axis. Oh remember that the y-value has to be in between 0 and 1 where 1 is the top of the plot. You can rescale your values accordingly. Another obvious option is simply

plt.plot([2.8, 2.8], [0, max(data)])

这篇关于Seaborn:如何向分布图添加垂直线 (sns.distplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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