2017-08-16 27 views
1

我正在用熊猫和pyplot绘制直方图。有关其他信息,我在直方图分布的特定百分点处添加了行。我已经发现,你可以做一个axvline出现与整个图表的某些%的高度:让axvline以某个y值结束

cycle_df = pd.DataFrame(results) 
plot = cycle_df.plot.hist(bins=30, label='Cycle time') 

plot.axvline(np.percentile(cycle_df,5), label='5%', color='red', linestyle='dashed', linewidth=2, ymax=0.25) 
plot.axvline(np.percentile(cycle_df,95), label='95%', color='blue', linestyle='dashed', linewidth=2, ymax=0.25) 

是否有可能让红/蓝线结束的确切位置直方图棒端太看起来光滑?

enter image description here

回答

1

这是绝对有可能,但我不知道这是否是容易pandas.DataFrame.hist做到,因为不返回直方图数据。你将不得不做另一个matplotlib.pyplot.hist(或numpy.hist)来获得实际的垃圾桶和高度。

但是如果你使用matplotlib直接这会工作:

import matplotlib.pyplot as plt 

plt.style.use('ggplot') 

import numpy as np 

data = np.random.normal(550, 20, 100000) 

fig, ax = plt.subplots(1, 1) 
plot = ax.hist(data, bins=30, label='Cycle time', color='darkgrey') 

ps = np.percentile(data, [5, 95]) 
_, ymax = ax.get_ybound() 

# Search for the heights of the bins in which the percentiles are 
heights = plot[0][np.searchsorted(plot[1], ps, side='left')-1] 

# The height should be the bin-height divided by the y_bound (at least if y_min is zero) 
ax.axvline(ps[0], label='5%', color='red', linestyle='dashed', linewidth=2, ymax=heights[0]/ymax) 
ax.axvline(ps[1], label='95%', color='blue', linestyle='dashed', linewidth=2, ymax=heights[1]/ymax) 
plt.legend() 

enter image description here

如果你不想计算的相对高度打扰,你也可以使用Lines2Dmatplotlib.lines

import matplotlib.pyplot as plt 
import matplotlib.lines as mlines 

plt.style.use('ggplot') 

import numpy as np 

data = np.random.normal(550, 20, 100000) 

fig, ax = plt.subplots(1, 1) 
plot = ax.hist(data, bins=30, label='Cycle time', color='darkgrey') 

ps = np.percentile(data, [5, 95]) 

# Search for the heights of the bins in which the percentiles are 
heights = plot[0][np.searchsorted(plot[1], ps, side='left')-1] 

# The height should be the bin-height divided by the y_bound (at least if y_min is zero) 
l1 = mlines.Line2D([ps[0], ps[0]], [0, heights[0]], label='5%', color='red', linestyle='dashed', linewidth=2) 
l2 = mlines.Line2D([ps[1], ps[1]], [0, heights[1]], label='95%', color='blue', linestyle='dashed', linewidth=2) 
ax.add_line(l1) 
ax.add_line(l2) 
plt.legend() 

enter image description here

+1

非常感谢!我使用了第二个选项,它完美地实现了它的目的。 – VeryMary