【问题标题】:create heatmap2d from txt file从 txt 文件创建 heatmap2d
【发布时间】:2013-09-02 19:18:01
【问题描述】:

我有一组 2d 数据 (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: bins 的维度必须等于样本 x 的维度。 脚本终止。

是否有任何方法可以打开 txt 文件并在 gnuplot、matplotlib 中绘制这些数据。 我的散点图看起来像这样

我想将此图片显示为带有颜色代码栏的等高线图或密度图。 我的 x 轴范围为 2.5-3.5 和 y 轴在 110-180 范围内 我有 30k 个数据点

【问题讨论】:

  • 您不能在gnuplot 本身中创建密度分布,这只能在一维中使用smooth kdensity。如果你想用 gnuplot 绘制数据,你必须使用其他工具(例如 Python)来预处理数据。

标签: python matplotlib plot gnuplot histogram


【解决方案1】:

如果你愿意在 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个高斯组成的测试数据,得到了这个结果:

【讨论】:

    【解决方案2】:

    以下是您可以如何使用 Python 预处理和使用 gnuplot 进行绘图的方法。

    变体 1

    第一个变体适用于 gnuplot 的 pm3d 绘图风格。这允许对直方图数据进行很好的插值,从而使图像看起来更平滑。但可能会给大型数据集带来问题,这也取决于输出图像格式(参见变体 2)。

    Python脚本process.py使用numpy.histogram2d生成直方图,输出保存为gnuplot的nonuniform matrix格式。

    # process.py
    from __future__ import print_function
    import numpy as np
    import sys
    
    M = np.loadtxt('datafile.dat', skiprows=1)
    bins_x = 100
    bins_y = 100
    H, xedges, yedges = np.histogram2d(M[:,0], M[:,1], [bins_x, bins_y])
    
    # output as 'nonuniform matrix' format, see gnuplot doc.
    print(bins_x, end=' ')
    np.savetxt(sys.stdout, xedges, newline=' ')
    print()
    
    for i in range(0, bins_y):
        print(yedges[i], end=' ')
        np.savetxt(sys.stdout, H[:,i], newline=' ')
        print(H[-1,i])
    
    # print the last line twice, then 'pm3d corners2color' works correctly
    print(yedges[-1], end=' ')
    np.savetxt(sys.stdout, H[:,-1], newline=' ')
    print(H[-1,-1])
    

    要绘图,只需运行以下 gnuplot 脚本:

    reset
    set terminal pngcairo
    set output 'test.png'
    set autoscale xfix
    set autoscale yfix
    set xtics out
    set ytics out
    set pm3d map interpolate 2,2 corners2color c1
    splot '< python process.py' nonuniform matrix t ''
    

    变体 2

    第二个变体使用image 绘图风格,这可能适用于大型数据集(大直方图大小),但看起来不太好,例如对于100x100矩阵:

    # process2.py
    from __future__ import print_function
    import numpy as np
    import sys
    
    M = np.loadtxt('datafile.dat', skiprows=1)
    bins_x = 100
    bins_y = 200
    H, xedges, yedges = np.histogram2d(M[:,0], M[:,1], [bins_x, bins_y])
    
    # remap xedges and yedges to contain the bin center coordinates
    xedges = xedges[:-1] + 0.5*(xedges[1] - xedges[0])
    yedges = yedges[:-1] + 0.5*(yedges[1] - yedges[0])
    
    # output as 'nonuniform matrix' format, see gnuplot doc.
    print(bins_x, end=' ')
    np.savetxt(sys.stdout, xedges, newline=' ')
    print()
    
    for i in range(0, bins_y):
        print(yedges[i], end=' ')
        np.savetxt(sys.stdout, H[:,i], newline=' ')
        print()
    

    要绘图,只需运行以下 gnuplot 脚本:

    reset
    set terminal pngcairo
    set output 'test2.png'
    set autoscale xfix
    set autoscale yfix
    set xtics out
    set ytics out
    plot '< python process2.py' nonuniform matrix with image t ''
    

    可能有一些地方需要改进(尤其是在 Python 脚本中),但它应该可以工作。我没有发布结果图像,因为您显示的几个数据点看起来很难看;)

    【讨论】:

    • 这个脚本正在运行,但它给出了散点图而不是密度或 histogram2d。
    • 我想用 histogram2d 制作等高线图。
    • 不,脚本给出了密度图。在您看来,是什么让使用process.py 生成的直方图是散点图而不是直方图?等高线图与热图完全不同。你想要只绘制轮廓,还是表面,还是其他东西?也许你应该发布一个示例图片,或者显示你真正想要的 URL...
    • 以上脚本是用python运行的。
    • 对不起,你搞砸了!你的答案应该是真实的答案吗?然后请将代码放入答案中,而不是作为评论。还是您的问题的另一个编辑?你应该用我的 gnuplot 脚本得到完全相同的结果。如果没有,请将完整的数据文件上传到某个地方。
    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2016-01-06
    • 2016-04-11
    • 2017-01-26
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多