Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a meshgrid for polar coordinates

I want to create a mesh grid for polar coordinates using the following arrays.

R = 1.15
r = numpy.linspace(R,5,100)
theta = numpy.linspace(0,2*numpy.pi,145)

I tried it this way, using numpy:

X,Y=numpy.meshgrid(r*numpy.cos(theta),r*numpy.sin(theta))

but I am getting this error:

ValueError: operands could not be broadcast together with shapes (100,) (145,) 

How do I generate the grid and display the points?

like image 954
bananagator Avatar asked Mar 08 '15 13:03

bananagator


1 Answers

Do not convert to a cartesian coordinate system if you simply want two 2D arrays that specify coordinates r and theta on a polar grid.

To clarify, the error you're seeing is because you can not perform element-wise multiplication between two arrays of unequal shape, which is what you have.

You should be calling it like this:

radius_matrix, theta_matrix = numpy.meshgrid(r,theta)

Then you could convert to cartesian coordinates (if really needed) by typing:

X = radius_matrix * numpy.cos(theta_matrix)
Y = radius_matrix * numpy.sin(theta_matrix)

Visualisation could be done on a polar grid immediately, using e.g. matplotlib:

import matplotlib.pyplot as plt
ax = plt.subplot(111, polar=True)
ax.plot(theta_matrix, radius_matrix, color='r', ls='none', marker='.')

Have a look at the polar plot demo if you'd like another example.

Alternatively, you could plot the polar grid you made by plotting the cartesian coordinates we obtained previously on a cartesian grid:

plt.plot(X,Y, 'r. ')
plt.show()
like image 155
Oliver W. Avatar answered Sep 30 '22 11:09

Oliver W.