2017-08-16 74 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