2013-07-02 13 views
3

Matplotlib的hist說「計算並繪製x的直方圖」。我想製作一個陰謀而不是首先計算任何東西。我有bin寬度(不相等)和每個bin中的總量,我想繪製一個頻率 - 數量直方圖。如何繪製不等寬的直方圖而不從原始數據計算出來?

例如,與數據

cm  Frequency 
65-75 2 
75-80 7 
80-90 21 
90-105 15 
105-110 12 

應該作出這樣的曲線圖:

Histogram

http://www.gcsemathstutor.com/histograms.php

其中塊的區域表示在每個頻率類。

回答

1

在相同的工作大衛Zwicker的:

import numpy as np 
import matplotlib.pyplot as plt 

freqs = np.array([2, 7, 21, 15, 12]) 
bins = np.array([65, 75, 80, 90, 105, 110]) 
widths = bins[1:] - bins[:-1] 
heights = freqs.astype(np.float)/widths 

plt.fill_between(bins.repeat(2)[1:-1], heights.repeat(2), facecolor='steelblue') 
plt.show() 

histogram using fill_between

1

你想要一個bar chart

import numpy as np 
import matplotlib.pyplot as plt 

x = np.sort(np.random.rand(6)) 
y = np.random.rand(5) 

plt.bar(x[:-1], y, width=x[1:] - x[:-1]) 

plt.show() 

這裏x包含條的邊緣和y包含高度(而不是區域!)。請注意,x中有一個元素比y多一個元素,因爲還有一個邊線比有條塊更多。

enter image description here

隨着原始數據和麪積計算:

from __future__ import division 
import numpy as np 
import matplotlib.pyplot as plt 

frequencies = np.array([2, 7, 21, 15, 12]) 
bins = np.array([65, 75, 80, 90, 105, 110]) 

widths = bins[1:] - bins[:-1] 
heights = frequencies/widths 

plt.bar(bins[:-1], heights, width=widths) 

plt.show() 

histogram with unequal widths

+0

有沒有辦法像'histt​​ype ='stepfilled''那樣獲得同樣的效果? – endolith