如何为三列数据绘制热图 [英] how to plot a heat map for three column data

查看:96
本文介绍了如何为三列数据绘制热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三列文件,五百万行.就像

I have three column file,5 million lines. It is like

x,y,z
3,4,6.7
9,4,7.8

X和y是像素数,z是(x,y)处的对应值
如何绘制热图?
2D图是我最初想法的折衷.
你可以查看我的原帖如何使用scipy.interpolate中的网格数据

X and y are pixel numbers and z are corresponding values at (x,y)
How to plot a heat map?
A 2D plot is a compromise for my original thought.
You can check my original post How to use griddata from scipy.interpolate

我尝试了以下方法,但这只是一个散点图.

I tried the way below but it is just a scatter point plot.

import numpy as np
import pylab as pl
x,y,z =np.loadtxt('3columns.csv',delimiter=',',usecols=(0,1,2),unpack=True)

pl.scatter(x, y, c=z)

pl.show()

推荐答案

我也遇到过类似的问题.我所做的是设置一个数组Z[row[0]][row[1]] = row[2].

I've encountered similar problems. What I did is to set an array Z[row[0]][row[1]] = row[2].

import numpy as np
x,y,z =np.loadtxt('3columns.csv',delimiter=',',usecols=(0,1,2),unpack=True)
nx = x.max() - x.min() + 1
ny = y.max() - y.min() + 1
Z = np.zeros((nx,ny)) 

assert x.shape == y.shape == z.shape
for i in range(len(x)):
    Z[x[i]-x.min()][y[i]-y.min()] = z[i] 

import matplotlib.pyplot as plt 
fig = plt.figure()
figure_name = 'figure_name'
plt.pcolor(np.arange(nx),np.arange(ny),Z,cmap=plt.cm.Reds)
plt.colorbar()
plt.xlim(0,x.max()-x.min())
plt.ylim(0,y.max()-y.min())

xlabels = np.arange(x.min(),x.max(),Nspacingx) # define Nspacing accordingly 
ylabels = np.arange(y.min(),y.max(),Nspacingy) 
plt.xticks(np.arange(0,x.max()-x.min(),Nspacingx),xlabels)
plt.yticks(np.arange(0,y.max()-y.min(),Nspacingy),ylabels)

plt.savefig(figure_name,dpi=400)

通过这种方式,您可以从 3 列数据文件绘制二维热图.

In this way, you can plot a 2D heatmap from a 3-column data file.

这篇关于如何为三列数据绘制热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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