从列表列表创建二维直方图 [英] Create 2d histogram from a list of lists

查看:81
本文介绍了从列表列表创建二维直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从两个数据数组创建一个二维直方图,一个带有 y 值范围 (rdata) 的列表,另一个是嵌套列表,其中外部列表​​给出特定时间的强度,内部列表给出给出高度范围之一中特定时间的强度.

I'm attempting to create a 2d histogram from two data arrays, one with a list with y value ranges (rdata) and other which is a nested list where the outer list gives the intensity at a specific time and the inner list givs the intensity at a specific time in one of the height ranges.

我正在尝试使用 y 轴上的范围值和 x 轴上的强度值创建一个二维直方图.我查看了 numpy.histogram2d 文档,并尝试从我使用 simpler 数组创建的模板文件重建此直方图.

I'm attempting to create a 2d histogram with the range values on the y axis and the intensity values on the x axis. I've looked at the numpy.histogram2d documentation and tried to reconstruct this histogram from a template file I created with a simmpler array.

附上代码:

import numpy as np
import file_reader as fr
import matplotlib.pyplot as plt

time = []
rdata = []
intensity = []

fr.file_reader(time, rdata, intensity)
print('TIME DATA:', time)
print('RANGE DATA:', rdata)
print('INTENSITY DATA:', intensity)

range_bins = np.linspace(rdata[0],rdata[1],len(rdata))
intensity_bins = np.linspace(-70,30,len(intensity))
rdata = [rdata]

for i in range((len(time)-1)):
    rdata.append(rdata)

print(rdata)


H, intensity_bins, rdata_bins = np.histogram2d(intensity,rdata,bins=(intensity_bins,range_bins))

X,Y = np.meshgrid(intensity_bins,range_bins)
plt.xlim(intensity_bins[-1])
plt.ylim(range_bins[-1])
plt.pcolormesh(X,Y,H, cmap='Reds')
plt.draw()

file_reader 是我创建的一个文件,用于从 .txt 中读取数据,为了简单起见,我将包含其中的数据输出,它构建了我要绘制的数组:

file_reader is a file I created to read data from a .txt and for simplicities sake I will include the data outputs from that, which construct the arrays I'm trying to plot:

TIME DATA: [16.23638916015625, 16.23916625976562, 16.24194526672363, 16.24472236633301, 16.24749946594238, 16.25027847290039, 16.25305557250977]
RANGE DATA: [155.89599609375, 187.0751953125, 218.25439453125, 249.43359375, 280.61279296875, 311.7919921875]
INTENSITY DATA: [[nan, nan, nan, nan, nan, nan], [nan, nan, -59.63091278076172, -49.99733352661133, nan, nan], [nan, 4.0, -3.2, -20.0, -20.0, -20.0], [1.1, nan, nan, nan, nan, nan], [nan, nan, -59.63091278076172, -49.99733352661133, nan, nan], [nan, 4.0, -3.2, -20.0, -20.0, -20.0], [5.32, -29.48, -50.0, -32.2, -1.111, -51.3]]

for i in range(len(time)-1) 函数是我测试以查看是否复制范围数据列表使其长度相同并且还包含一组嵌套列表将有助于将数据输入到直方图中,但是当我运行代码时,没有输出,它似乎卡住了,因为我必须按 ctrl+c 来停止代码运行.在这里坐了十分钟,它没有产生任何输出或完成运行,这是可笑的,因为这些都是小数据数组.

The for i in range(len(time)-1) function was me testing to see if copying the range data list so it was the same length and also contained a nested set of lists would help for data input into the histogram, but when I run the code there is no output and it seems to get stuck as I have to ctrl+c to stop the code running. Sat here for ten minutes and it didn't produce any output or finish running, which is ludicrous as these are small data arrays.

任何帮助将不胜感激.

推荐答案

numpy.histogram2D 要求 XY 是一维数组每个点的 x 和 y 坐标.从您提供的数据输出来看,您的 intensity 数组看起来是二维的.此外,当您运行 rdata 附加循环时,您附加的是中间值(即 2D).我已经通过使用 ravel 函数使 intensity 成为一维数组并使用 repeat 创建 rdata 数组来更改您的代码:

numpy.histogram2D requires X and Y to be 1D arrays of the x and y coordinates of each point. From the data outputs you've given, it looks like your intensity array is 2D. Also, when you are running through your rdata append loop, you are appending the intermediate value (which is 2D). I've changed your code by using the ravel function to make intensity a 1D array and using repeat to create the rdata array:

import numpy as np
import matplotlib.pyplot as plt

time =[16.23638916015625, 16.23916625976562, 16.24194526672363,
    16.24472236633301, 16.24749946594238, 16.25027847290039, 16.25305557250977]
rdata=[155.89599609375, 187.0751953125, 218.25439453125, 249.43359375,
    280.61279296875, 311.7919921875]
intensity= [[np.NaN, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN], 
    [np.NaN, np.NaN, -59.63091278076172, -49.99733352661133, np.NaN, np.NaN], 
    [np.NaN, 4.0, -3.2, -20.0, -20.0, -20.0], [1.1, np.NaN, np.NaN, np.NaN, np.NaN, np.NaN], 
    [np.NaN, np.NaN, -59.63091278076172, -49.99733352661133, np.NaN, np.NaN],
    [np.NaN, 4.0, -3.2, -20.0, -20.0, -20.0], [5.32, -29.48, -50.0, -32.2, -1.111, -51.3]]


range_bins = np.linspace(rdata[0],rdata[-1],len(rdata))
intensity_bins = np.linspace(-70,30,len(intensity))

intensity = np.array(intensity).ravel()

rdata = np.repeat(rdata,len(time))

H, intensity_bins, range_bins = np.histogram2d(intensity,rdata,
    bins=(intensity_bins,range_bins))

plt.imshow(H, interpolation='nearest', origin='low',
    extent=[intensity_bins[0], intensity_bins[-1],range_bins[0], range_bins[-1]])

这篇关于从列表列表创建二维直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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