我有数据表示圆圈内部点的值。我想创建一个类似于http://matplotlib.org/examples/pylab_examples/image_interp.html的热图。有人熟悉用圆圈做这个的方法吗?
答案 0 :(得分:2)
您可以在轴上使用极坐标投影来完成此操作。请注意,根据您提供的示例,这不适用于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。
如果你想做同样的事情,你需要在做同样的事情之前将你的数据转换成极坐标(假设你的读数是笛卡尔坐标)。为此,您可以使用:
def convert_to_polar(x, y):
theta = np.arctan2(y, x)
r = np.sqrt(x**2 + y**2)
return theta, r
您可能会发现这些问题的答案也很有用: image information along a polar coordinate system Adding a colorbar to a pcolormesh with polar projection
第一个特别是有一个非常详细的答案。
答案 1 :(得分:1)
我很欣赏这是一个古老的问题,但是当我利用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张图像,即插值前的图像:
以及后插值图:
就像我说的那样,这只有在系统地收集数据的情况下才有效,但是出于科学目的,情况将会如此。
另外,使用熊猫数据框不是必需的步骤,但是从这种角度看,从概念上讲,它要简单得多。
希望这会有所帮助。