使用imshow()创建更高质量的hist2d

时间:2017-07-13 18:59:03

标签: python image image-processing matplotlib

我正在玩体积数据,我正在尝试投射像图像一样的“宇宙网”。

我几乎创建了一个文件路径并使用打开hdf5文件的模块打开数据。 xy值通过文件gas_pos的索引来表示,直方图由不同的属性加权,在这种情况下为gas_density

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.ticker import LogFormatter 


cmap = LinearSegmentedColormap.from_list('mycmap', ['black', 'steelblue', 'mediumturquoise', 'darkslateblue'])

fig = plt.figure()
ax = fig.add_subplot(111)

H = ax.hist2d(gas_pos[:,0]/0.7, gas_pos[:,1]/0.7, bins=500, cmap=cmap, norm=matplotlib.colors.LogNorm(), weights=gas_density);

cb = fig.colorbar(H[3], ax=ax, shrink=0.8, pad=0.01, orientation="horizontal", label=r'$ \rho\ [M_{\odot}\ \mathrm{kpc}^{-3}]$')
ax.tick_params(axis=u'both', which=u'both',length=0)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

plt.show()

给我这个:

enter image description here

这很好,但我希望提高质量并消除它的颗粒感。当我尝试imshow插值时:

cmap = LinearSegmentedColormap.from_list('mycmap', ['black', 'steelblue', 'mediumturquoise', 'darkslateblue'])

fig = plt.figure()
ax = fig.add_subplot(111)
H = ax.hist2d(gas_pos[:,0]/0.7, gas_pos[:,1]/0.7, bins=500, cmap=cmap, norm=matplotlib.colors.LogNorm(), weights=gas_density);

ax.tick_params(axis=u'both', which=u'both',length=0)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

im = ax.imshow(H[0], cmap=cmap, interpolation='sinc', norm=matplotlib.colors.LogNorm())
cb = fig.colorbar(H[3], ax=ax, shrink=0.8, pad=0.01, orientation="horizontal", label=r'$ \rho\ [M_{\odot}\ \mathrm{kpc}^{-3}]$')

plt.show()

enter image description here

我错误地使用了这个吗?或者有什么更好的东西我可以用来修改像素化?

如果有人想要播放我的数据,我会在今天晚些时候上传数据!

2 个答案:

答案 0 :(得分:1)

您可能希望在调用interpolation='None'时设置imshow,而不是interpolation='sinc'

答案 1 :(得分:1)

使用interpolation='sinc'确实是一种平滑情节的好方法。其他人会例如是"gaussian""bicubic""spline16"

您观察到的问题是imshow绘图绘制在hist2d绘图的顶部,因此取其轴限制。这些限制似乎小于imshow图中的点数,因此您只能看到总数据的一部分。

解决方案要么根本不绘制hist2d图,要么至少绘制成另一个子图或图。

追求第一个想法,你可以使用numpy.histogram2d

计算你的直方图而不用它来绘制它
H, xedges, yedges = np.histogram2d(gas_pos[:,0]/0.7, gas_pos[:,1]/0.7,
                                   bins=500, weights=gas_density)
im = ax.imshow(H.T, cmap=cmap, interpolation='sinc', norm=matplotlib.colors.LogNorm())

我还建议阅读numpy.histogram2d文档,其中包括在matplotlib中绘制直方图输出的示例。

相关问题