2020年8月22日 星期六

[ 常見問題 ] Seaborn: How to add vertical lines to a distribution plot (sns.distplot)

 Source From Here

Quesiton
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:
  1. import pandas as pd  
  2. import numpy as np  
  3. import seaborn as sns  
  4. import matplotlib.pyplot as plt  
  5.   
  6. # some settings  
  7. sns.set_style("darkgrid")  
  8.   
  9. # Create some data  
  10. data = np.random.multivariate_normal([00], [[52], [22]], size=2000)  
  11. data = pd.DataFrame(data, columns=['x''y'])  
  12.   
  13. # Combined distributionplot  
  14. sns.distplot(data['x'])  
  15. 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).

How-To
Just use matplotlib.axes.Axes.axvline:
  1. plt.axvline(2.80,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
  1. plt.plot([2.82.8], [0, max(data)])  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...