Python 从txt文件创建heatmap2d
我有一组2d数据(30K)作为txt文件Python 从txt文件创建heatmap2d,python,matplotlib,plot,gnuplot,histogram,Python,Matplotlib,Plot,Gnuplot,Histogram,我有一组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
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绘制了散点图,但我想表示为热图2D或密度分布。
我查看了MatPlotLib或R中的示例,它们似乎都是从随机数据开始生成图像的
我尝试了这些代码,得到了如下错误
hist,Edge=histogramdd([x,y],箱子,范围,赋范,权重)
AttributeError:箱子的尺寸必须等于样本x的尺寸。
脚本终止
是否有任何方法可以打开txt文件并在gnuplot、matplotlib中打印此数据。
我的散点图是这样的
我想显示这张图片作为等高线图或密度图与颜色代码栏。
我的x轴范围为2.5-3.5
y轴在110-180范围内
我有30k个数据点以下是如何使用Python预处理和gnuplot绘图 变式1 第一个变体与gnuplot的
pm3d
打印样式配合使用。这允许对直方图数据进行很好的插值,从而使图像看起来更平滑。但可能会给大型数据集带来问题,这也取决于输出图像格式(参见变体2)
Python脚本process.py
使用numpy.histogram2d
生成直方图,输出保存为gnuplot的非均匀矩阵
格式
# 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 ''
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 ''
要打印,只需运行以下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 ''
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 ''
重置
设置终端pngcairo
设置输出“test2.png”
设置自动缩放xfix
设置自动缩放yfix
放线
出发
用图像t绘制“
可能有一些地方需要改进(特别是在Python脚本中),但它应该可以工作。我不会发布结果图像,因为它看起来很难看,因为您显示的数据点很少(
;)代码>如果您愿意用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个高斯组成的测试数据,得到了这个结果:
您无法在gnuplot
本身中创建密度分布,只有使用smooth kdensity
才能在1D中创建密度分布。如果要使用gnuplot绘制数据,必须使用其他工具(例如Python)对数据进行预处理。此脚本正在工作,但它提供的散点图不是密度图或Historogram2D。我想从Historogram2D绘制等高线图。不,脚本提供的是密度图。在您看来,是什么使得使用process.py
生成的直方图成为散点图而不是直方图?等高线图和热图是完全不同的。您希望只绘制等高线,还是同时绘制曲面,或者其他什么?也许你应该发布一个示例图像,或者显示你真正想要的URL…上面的脚本是用python运行的。对不起,你把事情搞砸了!你的答案应该是真实的吗?那么请在答案中输入代码,而不是作为注释。还是你的另一个问题?使用我的gnuplot脚本应该会得到完全相同的结果。如果没有,请将完整的数据文件上传到某处。