颜色编码的2D直方图 [英] Color-coded 2D histogram

查看:131
本文介绍了颜色编码的2D直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个颜色编码的直方图,我在其中输入一个数组数组来表示y轴上的元素,而在x轴上输入一个简单的一维数组来表示一个相.

I want to plot a color-coded histogram, where I input an array of arrays to represent the elements on the y-axis, while a simple 1D array on the x-axis to represent a phase.

要在y轴上绘制的数组的数组具有一个维度,例如(100, 25),而在x轴上的相具有25个元素.因此,100是25个相位仓中每个相位仓必须进行颜色编码的元素的数量.

The array of arrays to plot on the y-axis has a dimension, let's say, (100, 25), while the phase on the x-axis has 25 elements. Therefore, 100 is the number of elements that have to be color-coded for each of the 25 phase-bins.

我认为 numpy.hist2d 适合于此,但是它只需要两个相同大小的数组作为输入.我想我必须为每个100元素的25数组创建一个颜色映射?

I thought numpy.hist2d was suitable for this, but it only takes two same-sized arrays as input. I suppose I have to create a map of colors for each of the 25 arrays of 100 elements?

我真的不知道该如何处理,因为我完全没有颜色编码图的经验.

I really do not know how to approach this, because I have no experience with color-coded plots at all.

我找到了此示例非常接近我的情况,除了我想要一个2D绘图,其中Z维度是颜色:

I found this example as quite close to my case, except that I want a 2D plot where the Z-dimension is the color:

此外,不同的直方图需要具有相同的颜色编码.这是我的数据示例:

Also, the different histograms need to have the same color-coding. Here an example of my data:

 phase (X-axis) =  [ 0.01952176  0.04740999  0.07529822  0.10318645  0.13107468  
 0.15896291 0.18685114  0.21473937  0.2426276   0.27051583  0.29840406 
 0.32629229 0.35418052  0.38206875  0.40995698  0.43784521  0.46573344  
 0.49362167 0.5215099   0.54939813  0.57728636  0.60517459  0.63306282  
 0.66095105 0.68883928  0.71672751  0.74461574  0.77250397  0.8003922   
 0.82828043 0.85616866  0.88405689  0.91194512  0.93983335  0.96772158  
 0.99560981] 
 data to be color-coded in histograms (Y-axis) = [[ 0.01011273  0.00237802 -0.00227542 ...,         nan         nan          nan]
 [-0.00407017 -0.00317593 -0.00605734 ...,         nan         nan
      nan]
 [ 0.0166795   0.00798681  0.00075688 ...,         0.01022334         nan
      nan]
 ..., 
 [ 0.00940512         nan         nan ...,         nan         0.00022334
      0.00134779]
 [ 0.00176177  0.00151938         nan ...,         0.05692114         0.00021122
      -0.00003121]
 [        nan  0.00455727         nan ...,         0.06812121         0.00011512
      0.00016711]]

推荐答案

如果最后我没有正确理解,则您有一个数组(25,100),并且想要计算每行100个数据点的分布. 可能有一种使用hist2d的方法,但是我不知道如何使用它,所以这是我的方法:

If I understood correctly in the end, you have an array (25,100), and you want to calculate the distribution of the 100 data points for each row. There's probably a way to use hist2dfor this, but I don't know how to use it, so here would be my method:

Nphase = 25
Npoints = 100

phase = np.linspace(0.,1.,num=Nphase)
data = np.array([A*np.random.normal(size=(Npoints,))+C for (A,C) in 
                 zip(
            np.random.randint(1,2,Nphase),
            np.random.randint(-5,5,Nphase))])
#sprinkle some NaN
for i,j in zip(np.random.randint(0,Nphase,size=(10,)),np.random.randint(0,Npoints,size=(10,))):
    data[i,j] = np.NaN

您什么都没有说,也不涉及数据相对于彼此的缩放比例.在这里,我将使用20个bin并以相同的限制进行直方图绘制.

You don't say anything about the range of your data, or how they scale relative to one another. Here I'm going to do an histogram with 20 bins, and with the same limits.

#calculate the bins we're going to use
minBin, maxBin = np.nanmin(data),np.nanmax(data)
Nbins = 20

通过遍历每一行来计算直方图

calculate the histogram by iterating over each row

binedData = np.zeros((Nphase,Nbins))
for i,a in enumerate(data):
    binedData[i,:], bins = np.histogram(a[~np.isnan(a)],bins=Nbins,range=(minBin,maxBin))

情节

plt.matshow(binedData.T, cmap=plt.cm.RdYlBu_r, extent=(0,Nphase,maxBin,minBin))
plt.grid(False)
c = plt.colorbar(orientation='horizontal')
plt.xlabel('Phase')
plt.ylabel('bins')
c.set_label('Frequency')

现在,您提到要标准化每一行.有几种方法可以做到这一点,最好的方法是创建一个归一化的直方图,其中曲线下的面积等于1(请参见histogram函数的density参数). 在这里,我假设您只是为了可视化而希望最大值都相等.

Now, you mentioned that you want to normalize each row. There are several ways to do this, the best way would be to create a normalized histogram where the area under the curve is equal to 1 (see density argument to the histogram function). Here I assumed you just want the max to all be equal for visualisation sake.

# normalize histogram
data2 = 1.*(binedData - np.nanmin(binedData,axis=1, keepdims=True)) / (np.nanmax(binedData,axis=1,keepdims=True)-np.nanmin(binedData,axis=1,keepdims=True))

plt.matshow(data2.T, cmap=plt.cm.RdYlBu_r, extent=(0,Nphase,maxBin,minBin))
plt.grid(False)
c = plt.colorbar(orientation='horizontal')
plt.xlabel('Phase')
plt.ylabel('bins')
c.set_label('Frequency')

这篇关于颜色编码的2D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆