简体   繁体   English

使用3列数据创建轮廓图或热图

[英]Create Contour or Heat Map with 3 Columns of Data

I have data with 3 columns separated by commas in a file 'data.txt' 我在文件'data.txt'中有3列用逗号分隔的数据

x,y,z
12,12,5.2
12,26,12.1
12,40,3.5

Where x and y are the (x,y) coordinates (range 12-2000) and z is the value/intensity at that point. 其中x和y是(x,y)坐标(范围12-2000),z是该点的值/强度。 What is the best way to graph this data? 绘制这些数据的最佳方法是什么?

My initial thought was plotting as a 3-D contour plot and view it down the Z-axis, but even that is giving me some issues. 我最初的想法是将其绘制为3-D等高线图,并沿Z轴向下查看,但这甚至给我带来了一些问题。 I've made due plotting this as an array and plotting using imshow, but I know there's a better way. 我已经将其绘制为数组并使用imshow进行了绘制,但是我知道有更好的方法。 What advice do you have? 你有什么建议?

Attached is a my output using imshow. 附件是使用imshow的我的输出。 It works, but it's limited, as soon I will need to change my axes. 它可以工作,但有一定的局限性,因为我需要立即更改轴。 在此处输入图片说明

This my current code, but I know something needs to change 这是我当前的代码,但我知道需要更改

fig = plt.figure(2)
cmap2 = colors.LinearSegmentedColormap.from_list('my_colormap',['red','yellow','green'],256)
img2 = plt.imshow(data1,interpolation='nearest',cmap = cmap2, norm=MidpointNormalize(midpoint=p50)
        ,extent=[0.0009,3621085,0.0009,3621085], origin='lower')
cbar=plt.colorbar(img2,cmap=cmap2)
ax = plt.subplot(111)
ax.set_yscale('log')
ax.set_xscale('log')
xposition = [1,3.9,62.5,2000,64000,256000]
for xc in xposition:
        plt.axvline(x=xc, color='k', linestyle=':')
        plt.axhline(y=xc, color='k', linestyle=':')
img2 = plt.imshow(data1,interpolation='nearest',cmap = cmap2, norm=MidpointNormalize(midpoint=p50)
    ,extent=[12,2000,12,2000], origin='lower')
plt.colorbar(img2,cmap=cmap2)
fig.savefig(filenameI)
plt.close() 

The current way I was plotting my data means the values for x and y are independent of how I graph it. 我当前绘制数据的方式意味着x和y的值与我如何绘制图形无关。 I could make those axes say absolutely anything. 我可以使那些轴绝对说什么。 In contrast, I would like to graph these data and have them rely on the x- and y-values in my data table, because I will have to change my units at some point. 相比之下,我想绘制这些数据并使其依赖于数据表中的x和y值,因为在某些时候我将不得不更改单位。 How do I do that? 我怎么做?

Using imshow is an appropriate way to plot data on an equally spaced grid. 使用imshow是在等距网格上绘制数据的合适方法。 In order to link between the underlying grid and the axes in imshow , the extent keyword may be used 为了在基础网格和轴之间连结imshow ,所述extent可使用关键字

plt.imshow(data1, extent=[x.min(), x.max(), y.min(), y.max()], ...)

Other options to plot the data may be pcolor or pcolormesh . 绘制数据的其他选项可能是pcolorpcolormesh A nice comparisson between those in term of their basic usage is found as an example on the matplotlib page. 在matplotlib页面上,可以找到一个比较它们的基本用法的例子。

Some further reading on the differences: 关于差异的一些进一步阅读:

Essentially, pcolor is much slower than pcolormesh and imshow . 本质上, pcolorpcolormeshimshow慢得多。 Which of the later two to use is merely a question of taste. 使用后两个中的哪个仅是口味的问题。 pcolormesh also supports non-equal spaced grids and they differ in their default aspect settings. pcolormesh还支持非等距网格,并且它们的默认纵横比设置有所不同。

An alternative method to show data on a 2D grid is a contour plot , using contourf . 显示在二维网格数据的另一种方法是一个等高线图 ,使用contourf Whether to use this kind of plot, one has to decide depending on the usage case. 是否使用这种绘图,必须根据使用情况来决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM