Python Seaborn:如何向分布图添加垂直线 (sns.distplot)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52334938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seaborn: How to add vertical lines to a distribution plot (sns.distplot)
提问by vestland
Using the examples from seaborn.pydata.organd the Python DataScience Handbook, I'm able to produce a combined distribution plot with the following snippet:
使用来自seaborn.pydata.org和Python DataScience Handbook的示例,我能够使用以下代码段生成组合分布图:
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'])
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).
我知道我可以用 matplotlib 来做到这一点动态直方图子图与线标记目标,但我真的很喜欢 seaborn 图的简单性,并想知道是否有可能更优雅地做到这一点(是的,我知道 seaborn 构建在 matplotlib 之上)。
Thank you for any suggestions!
感谢您的任何建议!
回答by Sheldore
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
在这里,您可以使用诸如maxx = max(data)
或类似的变量来放置分布的最大值,而不是 0.17 。2.8 是 x 轴上的位置。哦请记住,y 值必须介于 0 和 1 之间,其中 1 是图的顶部。您可以相应地重新调整您的值。另一个明显的选择是
plt.plot([2.8, 2.8], [0, max(data)])