2017-09-05 6 views
1

私は行列A(20374、1)を持っています。 このデータを使ってヒストグラム(DVH)を描きたいと思います。Pythonを使ってヒストグラムを描く

私のコードは以下の通りです。

edge_A = np.linespace(0, max(A), 1000) 
x_A = np.linespace(0.5*max(A)/1000, max(A), 1000) 
n_A = np.histogram(A, bins=edge_A) 
y_A = 1 - np.cumsum(n_A/len(A), axis=0) 
plt.figure() 
plt.plot(x_A, y_A) 
plt.show() 

N_Aタプルとlen(A)であるので、しかし、このコードはy_Aの行で動作されていないがintで、これは計算することができません。 また、n_Aの行は正しくないと思います。

どうすればこの問題を解決できますか。

うまく動作するこの部分にmatlabコードを付けています。

edge_A = 0:max(A)/1000:max(A); 
x_A = 0.5*max(A)/1000:max(A)/1000:max(A); 
n_A = hiscounts(A, edge_A) 
y_A = 1 - cumsum(n_A/length(A)); 
plot(x_A, y_A); 

答えて

0

問題はそのnp.histogramがタプルとして2つの値を返します:

Returns 
------- 
hist : array 
    The values of the histogram. See `density` and `weights` for a 
    description of the possible semantics. 
bin_edges : array of dtype float 
    Return the bin edges ``(length(hist)+1)``. 

これは動作するようです:

A = np.random.rand(100000) 
edge_A = np.linspace(0, max(A), 1000) 
x_A = np.linspace(0.5*max(A)/1000, max(A), 1000) 
(n_A, _) = np.histogram(A, bins=edge_A,) 
y_A = 1 - np.cumsum(n_A/len(A), axis=0) 
plt.figure() 
plt.plot(x_A[1:], y_A) 
plt.show() 

ちょうどmatplotlibのを使用する方が簡単です:

plt.hist(A, 1000, cumulative=True) 
0

データをプロット、あなたはmatplotlibの直接使用することができます。

import numpy as np 
import matplotlib.pylab as plt 

a = np.random.normal(loc=0.0, scale=1.0, size=int(1e4)) 
plt.figure() 
plt.hist(a) 
plt.show() 

enter image description here

関連する問題