我有一個數組的值,我想創建它的直方圖。我主要對低端數據感興趣,並希望在一個垃圾箱中收集每個300以上的數字。這個垃圾箱應該和其他所有垃圾箱一樣寬。我怎樣才能做到這一點?Matplotlib直方圖與高值收集箱
注:這個問題涉及到這樣一個問題:Defining bin width/x-axis scale in Matplotlib histogram
這是我試過到目前爲止:
import matplotlib.pyplot as plt
import numpy as np
def plot_histogram_01():
np.random.seed(1)
values_A = np.random.choice(np.arange(600), size=200, replace=True).tolist()
values_B = np.random.choice(np.arange(600), size=200, replace=True).tolist()
bins = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 600]
fig, ax = plt.subplots(figsize=(9, 5))
_, bins, patches = plt.hist([values_A, values_B], normed=1, # normed is deprecated and will be replaced by density
bins=bins,
color=['#3782CC', '#AFD5FA'],
label=['A', 'B'])
xlabels = np.array(bins[1:], dtype='|S4')
xlabels[-1] = '300+'
N_labels = len(xlabels)
plt.xlim([0, 600])
plt.xticks(25 * np.arange(N_labels) + 12.5)
ax.set_xticklabels(xlabels)
plt.yticks([])
plt.title('')
plt.setp(patches, linewidth=0)
plt.legend()
fig.tight_layout()
plt.savefig('my_plot_01.png')
plt.close()
這是結果,這並不好看:
然後我用xlim改了一行:
plt.xlim([0, 325])
結果如下:
它看起來或多或少我所想要的,但最後斌現在是不可見的。我錯過了哪個技巧來以25的寬度形象化這最後一個bin?
這是一個骯髒的黑客,但它的工程!我接受這個答案,直到有更好的答案出現。 – physicalattraction 2014-10-06 15:13:04