我有一组二维数据(30K)作为 txt 文件。
X Y
2.50 135.89
2.50 135.06
2.50 110.85
2.50 140.92
2.50 157.53
2.50 114.61
2.50 119.53
2.50 154.14
2.50 136.48
2.51 176.85
2.51 147.19
2.51 115.59
2.51 144.57
2.51 148.34
2.51 136.73
2.51 118.89
2.51 145.73
2.51 131.43
2.51 118.17
2.51 149.68
2.51 132.33
我用 gnuplot 绘制了散点图,但我想表示为 heatmap2d 或密度分布。 我查看了 MatPlotLib 或 R 中的示例,它们似乎都已经从随机数据开始生成图像。
我试过这些代码并得到这样的错误
hist, edges = histogramdd([x,y], bins, range, normed, weights)
AttributeError: The dimension of bins must be equal to the dimension of the sample x. Script terminated.
是否有任何方法可以打开 txt 文件并在 gnuplot、matplotlib 中绘制此数据。 我的散点图看起来像这样
我想将此图片显示为带有颜色代码条的等高线图或密度图。 我的 x 轴在 2.5-3.5 范围内 和 y 轴在 110-180 范围内 我有 30k 个数据点
最佳答案
如果您愿意用 Python 做所有事情,您可以在一个脚本中计算直方图并构建等高线图:
import numpy as np
import matplotlib.pyplot as plt
# load the data
M = np.loadtxt('datafile.dat', skiprows=1)
# compute 2d histogram
bins_x = 100
bins_y = 100
H, xedges, yedges = np.histogram2d(M[:,0], M[:,1], [bins_x, bins_y])
# xedges and yedges are each length 101 -- here we average
# the left and right edges of each bin
X, Y = np.meshgrid((xedges[1:] + xedges[:-1]) / 2,
(yedges[1:] + yedges[:-1]) / 2)
# make the plot, using a "jet" colormap for colors
plt.contourf(X, Y, H, cmap='jet')
plt.show() # or plt.savefig('contours.pdf')
我刚刚制作了一些由 2 个高斯分布组成的测试数据并得到了这个结果:
关于python - 从 txt 文件创建 heatmap2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18526485/