0

I have a (numpy) matrix with three columns, the first two being the position and the third the value. I would like to plot this matrix. I tried to use plt.imshow(...) but the axis are then the indexes of my matrix and not the position.

How can I do that?

EDIT : my matrix is like that :

array([[ -0.00000000e+00,   0.00000000e+00,   9.72157349e+01],
       [ -2.50000000e-02,   0.00000000e+00,   9.72157349e+01],
       [ -5.00000000e-02,   0.00000000e+00,   9.72157349e+01],
       ..., 
       [ -2.42500000e+00,   8.28630000e-05,   9.72157349e+01],
       [ -2.45000000e+00,   8.28630000e-05,   9.72157349e+01],
       [ -2.47500000e+00,   8.28630000e-05,   9.72157349e+01]])

with the first column beiing the x-position, the second one being the y-position and the third the value at the point.

1
  • I rolled back the edit that included the answer. The answer only needs to be in one place, as an answer. Commented May 29, 2014 at 22:53

2 Answers 2

1

Imshow requires you to recalculate the third column into a 2D matrix and extract the x and y axis first. Then you can use imshow. For imshow you also need to specify the boundaries (extent) like so:

plt.imshow(matrix, cmap=plt.cm.jet, aspect='auto', origin='lower', alpha=1,interpolation='none', extent=(xAxis[0],xAxis[-1], yAxis[0], yAxis[-1]))

plt.colorbar()
plt.show()

Then you should see the correct "positions"

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

6 Comments

I supposed that xAxis is my first column and yAxis my second. However, when I use your command, I get the right extent, but I still don't get the colormap that corresponds to my data (the third column).
Ok, for that you have to use plt.colorbar() in addition. I have included it in the code.
What I wanted to say is that with your solution I get !this. What I want is !that but with the correct extent (those of the first image).
are the axis values equally spaced?
Yes, that's why it works. How could I do it with non-equally spaced values?
|
0

Best to use a scatter plot and use the third column for the color value:

 A = your_matrix_as_numpy_array
 X,Y,Z = A.T # with Z the values at points x,y
 import matplotlib.pyplot as plt
 plt.scatter(X,Y,c=Z) 
 plt.colorbar()
 plt.show

Note that you can still make the points larger/smaller and change the shape etcetera

Comments

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.