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

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

本文介绍了使用3列数据创建轮廓图或热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件'data.txt'中有用逗号分隔的3列数据

I have data with 3 columns separated by commas in a file 'data.txt'

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

其中x和y是(x,y)坐标(范围12-2000),z是该点的值/强度。绘制此数据的最佳方法是什么?

Where x and y are the (x,y) coordinates (range 12-2000) and z is the value/intensity at that point. What is the best way to graph this data?

我最初的想法是将其绘制为3-D等高线图并沿Z轴查看,但即使如此,给我一些问题。我已经将其绘制为数组并使用imshow进行了绘制,但是我知道有更好的方法。您有什么建议?

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. I've made due plotting this as an array and plotting using imshow, but I know there's a better way. What advice do you have?

附加是使用imshow的输出。它可以工作,但是有一定局限性,因为我需要立即更改轴。

Attached is a my output using 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()

当前绘制数据方式的方式x和y的值与我如何绘制图形无关。我可以使那些轴绝对说什么。相比之下,我想绘制这些数据并使其依赖于我的数据表中的x和y值,因为在某些时候我将不得不更改单位。我怎么做?

The current way I was plotting my data means the values for x and y are independent of how I graph it. 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. How do I do that?

推荐答案

使用 imshow 是在同等条件下绘制数据的一种合适方法间隔的网格。为了链接基础网格和 imshow 中的轴,可以使用 extent 关键字

Using imshow is an appropriate way to plot data on an equally spaced grid. In order to link between the underlying grid and the axes in imshow, the extent keyword may be used

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

其他绘制数据的选项可能是 pcolor pcolormesh
一个不错的 matplotlib页面上的一个示例。

Other options to plot the data may be pcolor or pcolormesh.A nice comparisson between those in term of their basic usage is found as an example on the matplotlib page.

对差异的进一步理解:






  • matplotlib: difference between pcolor, pcolormesh and imshow
  • When to use imshow over pcolormesh?
  • Why is plt.imshow so much quicker than plt.pcolor ?
  • matplotlib.pcolor very slow. alternatives?

从本质上讲, pcolor pcolormesh imshow 。使用后两个中的哪个仅是口味的问题。 pcolormesh 还支持非等距网格,并且它们的默认纵横比设置有所不同。

Essentially, pcolor is much slower than pcolormesh and imshow. 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.

另一种显示方法2D网格上的数据是,使用 contourf 。是否使用这种绘图,必须根据使用情况来决定。

An alternative method to show data on a 2D grid is a contour plot, using contourf. Whether to use this kind of plot, one has to decide depending on the usage case.

这篇关于使用3列数据创建轮廓图或热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:03