Create Contour or Heat Map with 3 Columns of Data

北慕城南 提交于 2019-12-12 05:48:02

问题


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

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?

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?

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() 

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?


回答1:


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()], ...)

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.

Some further reading on the differences:

  • 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?

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.

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.



来源:https://stackoverflow.com/questions/42451721/create-contour-or-heat-map-with-3-columns-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!