python 中带有极坐标图的笛卡尔缩放

Cartesian zoom with polar plot in python

我正在尝试在极坐标中绘制一些数据(我目前使用的是极坐标投影):

我使用的代码如下:

import matplotlib.pyplot as plt
import numpy as np

# Create radial and angular array
r = np.linspace(1.0,10,11)
t = np.linspace(0.0,0.5*np.pi,101)

# Define the quantity that I want to plot
z = np.zeros((len(t),len(r)))
for yval in range(len(r)):
  z[:,yval] = np.cos(16.0*t)/r[yval]

#Create the figure
f = plt.figure(figsize=(13,8))
ax = plt.subplot(111, projection='polar')
ax.set_rorigin(-1)

#Plot the data
pcm = ax.pcolormesh(t,r,z.T,cmap = 'hot',shading='gouraud')
ax.set_xlim([0.0,0.5*np.pi])
ax.set_ylim([1.0,10.0])

#Add colorbar and show
bar = f.colorbar(pcm)
plt.show()

到目前为止我没有问题,但我想放大此图的特定区域。 但是,当我设置轴范围时,轴仍然是极坐标,因此我无法缩放域的“笛卡尔”区域(即方框)。

一个可能的选择是将数据转换为笛卡尔坐标,但是当我这样做时,我在域的内部失去了很多分辨率,这是我应该绝对避免的。

如何在不手动转换数据的情况下 select 极坐标绘图的矩形区域?如果我必须切换到笛卡尔坐标,是否有任何 matplotlib 或 python 函数可以在处理域内部区域的分辨率的同时执行此操作? 提前致谢

您可以自己创建一个在域内部具有更高分辨率的 X、Y 网格,并将其与 ax.pcolormesh()

一起使用
# Create radial and angular array
r = np.linspace(1.0,10,11)
t = np.linspace(0.0,0.5*np.pi,101)

# Define the quantity that I want to plot
z = np.zeros((len(t),len(r)))
for yval in range(len(r)):
    z[:,yval] = np.cos(16.0*t)/r[yval]


#Create the figure, bigger figsize to make the resulting plot square
f = plt.figure(figsize=(13,10))
ax = plt.subplot(111) # Drop back to XY coordinates


# Generate the XY corners of the colormesh
X = np.array([[ri*np.cos(j) for j in t] for ri in r])
Y = np.array([[ri*np.sin(j) for j in t] for ri in r])

#Plot the data
pcm = ax.pcolormesh(X,Y,z.T,cmap = 'hot',shading='gouraud')

#Add colorbar and show
bar = f.colorbar(pcm)
plt.show()

The figure from the question

The figure generated by code above