简体   繁体   English

Plot numpy.histogram2d()

[英]Plot numpy.histogram2d()

I am wondering if it is maybe possible to plot the output of numpy.histogram2d() using matplotlib.pyplot.hist2d? I am wondering if it is maybe possible to plot the output of numpy.histogram2d() using matplotlib.pyplot.hist2d? In the one-dimensional case, this can be done by using:在一维情况下,这可以通过使用来完成:

counts, bins = np.histogram(something, bins=no_bins, range=(range_min, range_max))

plt.hist(bins[:-1], bins, weights=counts)

Is there a similar solution for the two-dimensional case?二维情况下是否有类似的解决方案? I do not want to plot the 2d histo with the methods suggested on https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html The idea behind this is that I would like to apply some corrections to the inital histogram (ie bin-by-bin background subtraction using data from another 2d histogram) and then plot the corrected histogram. I do not want to plot the 2d histo with the methods suggested on https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html The idea behind this is that I would like to apply some corrections到初始直方图(即使用来自另一个 2d 直方图的数据逐个背景减法),然后 plot 校正直方图。

Many thanks in advance提前谢谢了

Plotting 2d histograms is typically done with imshow() .绘制二维直方图通常使用imshow() 完成 If you're used to ROOT or some other plotting libraries, be especially careful what argument you give for origin and extent .如果您习惯于 ROOT 或其他一些绘图库,请特别注意您为originextent提供的参数。

The following solution works as expected:以下解决方案按预期工作:

counts_bkg, bins_x_bkg, bins_y_bkg = np.histogram2d(x_bkg, y_bkg, bins=(x_bins, ybins))
counts, bins_x, bins_y = np.histogram2d(x, y, bins=(x_bins, y_bins))

diff = counts - counts_bkg
diffT = diff.T

fig, ax = plt.subplots(1)
pc = ax.pcolorfast(bins_x, bins_y, diffT)
plt.show()

In the docs , you can find three examples of how to plot the output from np.histogram2d() using the matplotlib functions imshow , pcolormesh and NonUniformImage . 在文档中,您可以找到三个示例,说明如何使用matplotlib函数imshowpcolormeshNonUniformImage绘制np.histogram2d()的输出。

在此处输入图片说明

I googled my way here.我在这里搜索了我的方式。 I am going to write here what worked for me, just in case, some else finds themselves in similar situtation.我将在这里写下对我有用的东西,以防万一其他人发现自己处于类似情况。

I was looking at the source code of pyplot, where I found reference to axes class.我正在查看 pyplot 的源代码,在那里我找到了对轴 class 的引用。 The hist2d funtion is actually defined in 'matplotlib/axes/_axes.py'. hist2d 函数实际上是在“matplotlib/axes/_axes.py”中定义的。 There I found hist2d calls np.histogram2s and then uses the xedges, yedges and bins in plt.pcolormesh as follow:在那里我发现 hist2d 调用 np.histogram2s 然后使用 plt.pcolormesh 中的 xedges、yedges 和 bin 如下:

pc = self.pcolormesh(xedges, yedges, h.T, **kwargs)

Remember there is no option to use keywords 'Range', 'density' and 'bins' in pcolormesh but these are taken into account by the np.histogram2d function.请记住,在 pcolormesh 中没有使用关键字“Range”、“density”和“bins”的选项,但 np.histogram2d function 会考虑这些。

TL;DR: Using pcolormesh is the simplest way for plotting 2D histogram from the output of np.histogram2d. TL;DR:使用 pcolormesh 是从 np.histogram2d 的 output 绘制 2D 直方图的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM