0

I am trying to plot a picture like this in python.

enter image description here

I have three parameters for ploting. x:

[ 0.03570416  0.05201517  0.05418171  0.01868341  0.07116423  0.07547471]

y:

 [-0.32079484 -0.53330218 -1.02866859 -0.94808545 -0.51682506 -0.26788337]

z:

[-0.32079484 -0.53330218 -1.02866859 -0.94808545 -0.51682506 -0.26788337]

so x is x-axis and y is y-axis. however z is the intensity of the pixel. I come up with this code:

z = np.array(reals)
x = np.array(ra)
y = np.array(dec)
nrows, ncols = 10, 10 
grid = z.reshape((nrows, ncols))
plt.imshow(grid, extent=(x.min(), x.max(), y.max(), y.min()), interpolation='nearest', cmap=cm.gist_rainbow)
plt.title('This is a phase function')
plt.xlabel('ra')
plt.ylabel('dec')
plt.show()

However I get this error:

grid = z.reshape((nrows, ncols))
ValueError: total size of new array must be unchanged

ra, dec and reals are normal arrays with the same size. I calculated them before and then I create the numpy arrays with them

1
  • 1
    Given your example data, x.size should, but does not, equal nrows * ncols. Commented Mar 27, 2013 at 10:57

1 Answer 1

1

The data you show is not consistent with making an image, but you could make a scatter plot with it.

The two basic types of plots for z values at (x,y) coordinate pairs are:

  1. scatter plots, where for each (x,y) pair, a z-value is specified.
  2. image (imshow, pcolor, pcolormesh, contour), where an x-axis with m regularly spaced values, and a y-axis with n regularly spaced values are specified, and then an array of z-values with size (m,n) is given.

Your data looks more like the former type, so I'm suggesting a scatter plot.

Here's what a scatter plot looks like (btw, your y and z values are the same, which if probably a mistake):

import numpy as np
import matplotlib.pyplot as plt

x = np.array([ 0.03570416, 0.05201517, 0.05418171, 0.01868341, 0.07116423, 0.07547471])
y = np.array([-0.32079484, -0.53330218, -1.02866859, -0.94808545, -0.51682506, -0.26788337])
z = np.array([-0.32079484, -0.53330218, -1.02866859, -0.94808545, -0.51682506, -0.26788337])

plt.scatter(x, y, c=z, s =250)        

plt.show()

enter image description here

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

1 Comment

If extending this to >1000 points you'll want to use the faster plot(x, y, marker="o", ls="") rather than scatter(x, y)

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.