Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

matplotlib - Circular interpolated heat map plot using python

I have data that represents values at interior points within a circle. I would like to create a heat map similar to http://matplotlib.org/examples/pylab_examples/image_interp.html. Anybody familiar with a method for doing this with a circle?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this by using a polar projection on your axis. Note, that this will not work with imshow as per the example you gave. (See: http://en.it-usenet.org/thread/15998/715/) However you can still do interpolation and then plot a heat map. Below is a simple example:

from pylab import *
import numpy as np
from scipy.interpolate import griddata

#create 5000 Random points distributed within the circle radius 100
max_r = 100
max_theta = 2.0 * np.pi
number_points = 5000
points = np.random.rand(number_points,2)*[max_r,max_theta]

#Some function to generate values for these points, 
#this could be values = np.random.rand(number_points)
values = points[:,0] * np.sin(points[:,1])* np.cos(points[:,1])

#now we create a grid of values, interpolated from our random sample above
theta = np.linspace(0.0, max_theta, 100)
r = np.linspace(0, max_r, 200)
grid_r, grid_theta = np.meshgrid(r, theta)
data = griddata(points, values, (grid_r, grid_theta), method='cubic',fill_value=0)

#Create a polar projection
ax1 = plt.subplot(projection="polar")
ax1.pcolormesh(theta,r,data.T)
plt.show()

Note that I have used a fill_value of 0, so any values in the grid that fall outside the convex shape of the random data will have the value of 0.

Interpolated Polar Heatmap If you wish to do the same you will need to convert your data into polar coordinates before doing the same, (assuming your readings are in Cartesian coordinates). To do that you can use:

def convert_to_polar(x, y):
    theta = np.arctan2(y, x)
    r = np.sqrt(x**2 + y**2)
    return theta, r 

You may find the answers to these questions helpful too: image information along a polar coordinate system Adding a colorbar to a pcolormesh with polar projection

The first of those in particular has a really detailed answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...