2016-11-17 32 views
0

繪製HIST數字I有兩個數據列表數據值(每個100個號碼)D1 = [0.5,0.6,0.45,........],D2 = [ 0.45,0.65,........]。我想用兩個列表數據繪製兩個hist,如下圖所示!如何用matplotlib繪製它,謝謝! 我的代碼如下:python3:如何由兩個列表數據與matplotlib

def plot_data(d1, d2): 
    fig, ax = plt.subplots() 
    ax.hist(d1, 100, 50, ec='red', fc='none', lw=1.5, histtype='step', label='n-gram') 
    ax.hist(d2, 100, 50, ec='green', fc='none', lw=1.5, histtype='step', label='ensemble') 
    ax.legend(loc='upper left') 
    plt.show() 

但有錯誤:

mn, mx = [mi + 0.0 for mi in range] 
TypeError: 'int' object is not utterable 

two hist with two data lists

+1

這是哪裏的代碼傳給你的錯誤。 – harshil9968

+1

什麼@ harshil9968的意思是,有什麼你給我們的錯誤有什麼密謀這樣的數據沒有證據。 – Gormador

回答

2

你的錯誤來自於第三個參數hist()功能。 Read the documentationhist()

Parameters: x : (n,) array or sequence of (n,) array (...)

bins : integer or array_like, optional (...)

range : tuple or None, optional

The lower and upper range of the bins. Lower and upper outliers are ignored. If not provided, range is (x.min(), x.max()). Range has no effect if bins is a sequence.**

If bins is a sequence or range is specified, autoscaling is based on the specified bin range instead of the range of x.

Default is None

第三個參數必須是tupleNone和您所提供的50家(int)。

看到下面的代碼,在這裏我簡單地通過None取代了第三個參數(並降低倉的數量)

d1 = np.random.normal(size=(100,)) 
d2 = 1+np.random.normal(size=(100,)) 

fig, ax = plt.subplots() 
ax.hist(d1, 10, None, ec='red', fc='none', lw=1.5, histtype='step', label='n-gram') 
ax.hist(d2, 10, None, ec='green', fc='none', lw=1.5, histtype='step', label='ensemble') 
ax.legend(loc='upper left') 
plt.show() 

enter image description here

+0

感謝您@Diziet旭回答 – tktktk0711

+0

如果我的回答對您有所幫助,請通過點擊選中標記左側考慮接受它 –