繁体   English   中英

使用python的圆形插值热图

[英]Circular interpolated heat map plot using python

我有表示圆内点处值的数据。 我想创建一个类似于http://matplotlib.org/examples/pylab_examples/image_interp.html的热图。 有人熟悉用圆圈做这个的方法吗?

您可以通过在轴上使用极坐标投影来做到这一点。 请注意,根据您提供的示例,这不适用于 imshow。 (请参阅: http : //en.it-usenet.org/thread/15998/715/ )但是您仍然可以进行插值,然后绘制热图。 下面是一个简单的例子:

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

请注意,我使用的 fill_value 为 0,因此网格中落在随机数据凸面形状之外的任何值都将具有 0 值。

Interpolated Polar Heatmap 如果你想这样做,你需要在做同样的事情之前将你的数据转换成极坐标(假设你的读数是笛卡尔坐标)。 为此,您可以使用:

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

您可能会发现这些问题的答案也很有帮助: 沿极坐标系的图像信息向具有极坐标投影的 pcolormesh 添加颜色条

特别是第一个有一个非常详细的答案。

我很欣赏这是一个老问题,但是当我利用 Weir_Doe 的答案并以稍微不同的方式开发它时,我想我会贡献我的方法,希望它可以帮助其他人。

我试图做一些类似的事情,并以系统的方式收集 r 和 theta 的结果,因此我最终得到了一个网格。 有了网格后,您可以使用缩放来获得更高清晰度的图像。

from pylab import *
import numpy as np
from scipy.ndimage import zoom
import pandas as pd

max_r = 100
max_theta = 2.5 * np.pi
number_points = 5

#Generate a grid 100 x 100 r x theta
r = np.arange(0, max_r,max_theta/number_points)
theta = np.arange(0,max_theta,max_theta/number_points)
grid_r, grid_theta = np.meshgrid(r, theta)

#Generate random numbers for each grid point
values = (np.sin(grid_r)+np.cos(grid_theta)).flatten()


#I always find it easier to put it in a dataframe
df = pd.DataFrame(grid_r.flatten()).rename(columns={0:'r'})
df['theta'] = grid_theta.flatten()
df['values'] = values
df = df.pivot(index='theta', columns='r')
#printing the dataframe at this point is very helpful conceptually

#Create a polar projection
ax1 = plt.subplot(projection="polar")
ax1.pcolormesh(df.index,r,df.values.T)
plt.show()

#Zoom in to the grid, this interpolates the results onto a finer grid
#Here I chose a 10x finer grid, this is more efficient than to interpolate onto specified points
zoom_factor=10

zoomed_df = zoom(df, zoom_factor)
zoomed_index = zoom(theta, zoom_factor)
zoomed_columns = zoom(r, zoom_factor)
high_def_grid = pd.DataFrame(zoomed_df, index=zoomed_index, columns=zoomed_columns)


#Create a polar projection
ax1 = plt.subplot(projection="polar")
ax1.pcolormesh(high_def_grid.index,high_def_grid.columns,high_def_grid.values.T)
plt.show()

这会产生 2 个图像,即预插值图像:

非插值极坐标图

和后插值图:

插值极坐标图

就像我说的,这只有在以系统的方式收集数据时才有效,但出于科学目的,情况确实如此。

此外,使用 Pandas 数据框并不是必要的步骤,但我发现这样做在概念上要简单得多。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM