I'm new to matplotlib (and am loving it!), but am getting frustrated. I have a polar grid represented as a a 2D array. (rows are radial sections, columns are azimuthal sections)
I've been able to display the 2D array as both a rectangular image (R vs. theta) using pyplot.imshow() and as a polar plot using pyplot.pcolor(). However, pcolor() is painfully slow for the size of the arrays I'm using, so I want to be able to display the array as a polar grid using imshow().
Using pcolor(), this is as simple as setting polar=True for the subplot. Is there any way to display the 2D array as a polar plot using imshow()? without having to do coordinate transformations on the entire array? Thanks in advance
imshow. The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .
A point in polar co-ordinates is represented as (r, theta). Here, r is its distance from the origin and theta is the angle at which r has to be measured from origin. Any mathematical function in the Cartesian coordinate system can also be plotted using the polar coordinates.
imshow() allows you to render an image (either a 2D array which will be color-mapped (based on norm and cmap) or a 3D RGB(A) array which will be used as-is) to a rectangular region in data space.
imshow( BW ) displays the binary image BW in a figure. For binary images, imshow displays pixels with the value 0 (zero) as black and 1 as white. example. imshow( X , map ) displays the indexed image X with the colormap map . example.
After some research I discovered the pcolormesh() function, which has proven to be significantly faster than using pcolor() and comparable to the speed of imshow().
Here is my solution:
import matplotlib.pyplot as plt
import numpy as np
#...some data processing
theta,rad = np.meshgrid(used_theta, used_rad) #rectangular plot of polar data
X = theta
Y = rad
fig = plt.figure()
ax = fig.add_subplot(111)
ax.pcolormesh(X, Y, data2D) #X,Y & data2D must all be same dimensions
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