1

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.enter image description here

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?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • It's not clear from the question what the problem is. The plot you show seems to be appropriate to visualize the data. If you want something different, you need to clearly state what kind of plot you want and what problem you have achieving it. Stackoverflow is not aimed at providing advice on questions in the form of *"hey, I've made a cheese cake, but it's not tasting good; what other cake could I bake?"*. – ImportanceOfBeingErnest Feb 25 '17 at 09:43
  • 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. 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. – TallnGinger Feb 28 '17 at 04:03

1 Answers1

0

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:

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.

Community
  • 1
  • 1
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712