使用python的圆形插值热图

使用python的圆形插值热图

本文介绍了使用python的圆形插值热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据,它们代表一个圆内的内部点的值.我想创建一个类似于 使用极坐标投影向 pcolormesh 添加颜色条

尤其是其中的第一个有一个非常详细的答案.

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?

解决方案

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.

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 systemAdding a colorbar to a pcolormesh with polar projection

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

这篇关于使用python的圆形插值热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:03