2

I have some data of the following type: grid = np.array([posx, posy]) where posx and posy are the X/Y position some, stored in another array. The (transposed) grid may look like:

grid = np.array([posx, posy])
print grid.T  
[[   2.47685286    2.51629155]
[   2.47685286    8.51629155]
[   2.47685286   14.51629155]
[   8.47685286    5.51629155]
[   8.47685286   11.51629155]
[  14.47685286    2.51629155]
[  14.47685286    8.51629155]
[  14.47685286   14.51629155]]

Especially the y-Position is not identical in each "row" and the number of points differs, which I assume to be one of my problems.

Additionally, the corresponding data is stored in another (1D-)array like data = [2.3 4.7 -0.3 .....] having the same amount of entrys as I have points. My aim is to plot this data in kind of a smooth heatmap displaying by colours indicating position of high / low values. So far I used:

import numpy as np
import matplotlib.pyplot as p
p.imshow(data, interpolation=None)
p.colorbar()
p.show()

Obviously my problem is that I need to adjust the positon of my points. I searched some other posts but with this shape of data it never worked out. Also I tried to adjust this by simply reshaping the data but this didn't work due to the irregular number of points

As I am new here I am also happy for comments on how to improve my post (more input needed etc.) Thanks in advance!

2 Answers 2

1

There are several solutions to this problem.

If what you want is simply to have the points shown as markers of some size, with colors depending on the values in the z array, then a scatterplot will do nicely. If the space between the points should also be colored however, you should use interpolation and contouring. Fortunately those things have also been implemented in matplotlib for irregularly spaced data (data on an "unstructured grid"), which is what you have as the points cannot be easily mapped to a regular grid (although in the small example you've given, there does seem to be a tendency for equal-sized triangles).

Here are 3 examples that illustrate the functions you might want to look further into: plt.scatter, plt.tripcolor and plt.tricontourf. I've made the dataset to play with a bit larger, so that you can get a feeling of the function that is represented by z.

x,y = (2*np.random.rand(50)-1 for _ in range(2))
z = np.exp(-x*x - y*y) - np.cos(x)  # turtle-surface around origin
f, ax = plt.subplots(1,3, sharex=True, sharey=True, num=2, subplot_kw={'xlim': (-1,1), 'ylim': (-1, 1)})

ax[0].scatter(x,y, s=500*(z-z.min()), c=z, cmap='hot') # scatterplot with variable-sized markers and colors
ax[1].tripcolor(x, y, z, cmap='hot') # creates a tesselation and colors the formed triangles based on the values in the 3 nodes
ax[2].tricontourf(x, y, z, cmap='hot') # estimates the underlying surface

for indx in (1,2):
    ax[indx].triplot(x,y, 'ko ') # add the locations of the points
for axes in ax: # turn off the needless clutter
    axes.tick_params(axis='both', which='both', bottom='off', left='off', labelbottom='off', labelleft='off')
ax[0].set_title('scatter')
ax[1].set_title('tripcolor')
ax[2].set_title('tricontourf')

illustration of plotting irregularly spaced (triangular) data

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! As you gave me some options, maybe I should have made more clear what I want: I am more interested in your last case or even in something displaying the colors without the points. Even if I measure single data points the underlying theory is smooth. So I guess I could just skip your line " # add the locations of the points", which I will try asap
However, I decided to go with the first answer, it might not explain a lot but fits a little better and seems to be simpler.
0

I think your problem could be solved by creating a regular 2d-matrix and then using scipy.interpolate to interpolate the data between your data points. Example can be found at: http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#id1

1 Comment

Thanks! Even though I still need to play around which of the settings work best, this is the kind of thing I am interested in!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.