简体   繁体   English

在python中使用极坐标图进行笛卡尔缩放

[英]Cartesian zoom with polar plot in python

I am trying to plot some data in polar coordinates (I am currently using the polar projection):我正在尝试在极坐标中绘制一些数据(我目前正在使用极坐标投影): 极坐标中的数据

The code I am using is the following:我使用的代码如下:

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

So far I have no problem, but I would like to zoom on a particular region of this plot.到目前为止,我没有问题,但我想放大该图的特定区域。 However, when I set the axes range the axes is still polar, therefore I cannot zoom on a "cartesian" region of the domain (ie a square box).但是,当我设置轴范围时,轴仍然是极坐标,因此我无法放大域的“笛卡尔”区域(即方框)。

A possible option would be to transform the data into cartesian coordinates, but when I do it I lose a lot of resolution in the inner part of the domain, which is something that I should absolutely avoid.一个可能的选择是将数据转换为笛卡尔坐标,但是当我这样做时,我会在域的内部失去很多分辨率,这是我绝对应该避免的。

How can I select a rectangular zone of a plot in polar coordinates without transforming by hand the data?如何在不手动转换数据的情况下选择极坐标图中的矩形区域? And in case I have to switch to cartesian coordinates, is there any matplotlib or python function that does it while taking care of the resolution in the inner regions of the domain?如果我必须切换到笛卡尔坐标,是否有任何 matplotlib 或 python 函数可以在处理域内部区域的分辨率时执行此操作? Thanks in advance提前致谢

You can create an X, Y mesh yourself that is has a higher resolution on the inner part of the domain and use that with ax.pcolormesh()您可以自己创建一个 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上面代码生成的图

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

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