从txt文件创建heatmap2d [英] create heatmap2d from txt file

查看:98
本文介绍了从txt文件创建heatmap2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将2d数据(30K)设置为txt文件.

I have set of 2d data (30K) as txt file.

  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中的示例,它们似乎都已经从随机数据开始生成图像.

I plotted as a scatter plot with gnuplot but I would like to represent as a heatmap2d or density distribution. I looked through the examples in MatPlotLib or R and they all seem to already start with random data to generate the image.

我尝试了这些代码,并得到了这样的错误

I tried those code and get error like this

hist,edge = histogramdd([x,y],bins,range,normed,weights)

hist, edges = histogramdd([x,y], bins, range, normed, weights)

AttributeError:容器的尺寸必须等于样本x的尺寸. 脚本已终止.

AttributeError: The dimension of bins must be equal to the dimension of the sample x. Script terminated.

是否有打开txt文件并将其绘制在gnuplot,matplotlib中的数据的方法. 我的散点图看起来像这样

Is there any methods to open txt file and plot this data in gnuplot, matplotlib. my scatter plot look like this

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

i want to show this picture as contour map or density map with color code bar. my x-axis at range of 2.5-3.5 and y axis at range of 110-180 i have 30k data points

推荐答案

如果您愿意使用Python进行所有操作,则可以在一个脚本中计算直方图并构建轮廓图:

If you're willing to do everything in Python, you can compute the histogram and build a contour plot in one script :

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

I just made up some test data composed of 2 Gaussians and got this result :

这篇关于从txt文件创建heatmap2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆