gpt4 book ai didi

python - 使用 matplotlib 绘制交互式直方图?

转载 作者:行者123 更新时间:2023-12-01 04:09:36 25 4
gpt4 key购买 nike

上下文:我有一张图像,其中显示 3 个 CCD,其中一堆 X 射线相互作用显示为明亮像素。我已将此数据绘制为直方图(对数据进行切片以隔离每个单独的 CCD)。

我得到一个如下所示的直方图:

enter image description here

左侧的大峰值是背景噪声(实际峰值进一步上升,但我更改了轴范围以显示直方图的其他部分),右侧应该有一个小得多的峰值(虽然这张图片上并没有真正可见),这就是 X 射线相互作用。

我的具体问题..有没有办法获得每个峰的标准差和平均值?理想情况下(我不确定这在Python中是否可行)是说,单击峰值的两侧并计算两次单击内数据的标准差和平均值?

我使用的是 python 3.4 和 matplotlib 版本 1.4.3

我的代码如下:

### read the data ###

a = np.fromfile(filename.img, dtype=np.uint32) #unpack the data and put it into a numpy array

image_data = np.reshape(a[3:], (-1,Cols))


### plotting the data ###

fig, ax = plt.subplots()
ax.imshow(image_data, cmap=cm.gray, interpolation='nearest', clim=(0,2000))


numrows, numcols = image_data.shape


def format_coord(x, y):
x = int(x +0.5)
y = int(y +0.5)
col = x
row = y
if col >= 0 and col < numcols and row >= 0 and row < numrows:
z = image_data[row, col]
return 'x=%1.0f, y=%1.0f, z=%1.0f' % (x, y, z)
else:
return 'x=%1.4f, y=%1.4f' % (x, y)


ax.format_coord = format_coord
plt.figure()


### Obtaining a histogram ###

which_CCD = int(input ('Which CCD? 1,2,3... '))

if which_CCD == 1:
CCD1_image = np.array(image_data[150:1500,300:1498])
CCD1_data=np.reshape(CCD1_image,np.product(CCD1_image.shape))
plt.hist(CCD1_data, bins = 2500, histtype='step')
plt.xlabel('Energy(ADC Channels)')
plt.title('CCD1 Histogram')
plt.figure()



elif which_CCD == 2:
CCD2_image = np.array(image_data[1800:3150,300:1498])
CCD2_data=np.reshape(CCD2_image,np.product(CCD2_image.shape))
plt.hist(CCD2_data, bins = 2500, histtype='step')
plt.xlabel('Energy(ADC Channels)')
plt.title('CCD2 Histogram', fontsize=12)
plt.figure()



elif which_CCD == 3:
CCD3_image = np.array(image_data[150:1500,1948:3146])
CCD3_data=np.reshape(CCD3_image,np.product(CCD3_image.shape))
plt.hist(CCD3_data, bins = 2500, histtype='step')
plt.xlabel('Energy(ADC Channels)')
plt.title('CCD3 Histogram')
plt.figure()




plt.show()

最佳答案

如果我理解正确的话,您想要选择一个 X 值范围并计算该范围内 Y 值的平均值和标准差。

这是一个简单的示例,说明如何做到这一点。我相信您可以轻松地根据您的需求进行调整。

import matplotlib.pyplot as plt
import numpy as np
import numpy.random as npr

# generate random data
data = npr.randn(300)

# calculate hist
heights,edges = np.histogram(data,50)

# center edges
edges = edges[:-1]+(edges[1]-edges[0])

# plot histogram
fig, ax = plt.subplots()
ax.plot(edges,heights)
ax.set(title='Click Twice', xlabel='X', ylabel='Y')

# get input from 2 clicks on figure
point1, point2 = fig.ginput(2)

# paint selected area in red
ax.axvspan(point1[0], point2[0], color='red', alpha=0.5)

# calculate which values are selected and display mean and std
mask = (edges>point1[0]) & (edges<point2[0])
fig.text(0.2,0.83,'Mean: ' + str(np.mean(heights[mask])))
fig.text(0.2,0.8,'Std: ' + str(np.std(heights[mask])))

fig.canvas.draw()
plt.show()

输出:

带有选择点提示的直方图 enter image description here

绘制选定区域并显示平均值和标准差 enter image description here

附注我用过this response .

P.P.S。我使用的是 Python 2.7,Python 3 可能需要进行一些语法更改。

关于python - 使用 matplotlib 绘制交互式直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com