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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With