2017-09-05 150 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() 

但是,这个代码是不是在Y_A的线工作,因为N_A是元组和len(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返回两个值的元组:

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