Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display 2D array as polar plot using Matplotlib imshow()?

Tags:

matplotlib

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

like image 518
John Avatar asked Jun 23 '11 01:06

John


People also ask

What is Imshow in Matplotlib?

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() .

How do you plot a polar plot in Python?

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.

What does ax Imshow do?

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.

What is Imshow?

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.


1 Answers

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()
like image 106
John Avatar answered Oct 09 '22 01:10

John