You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

极坐标转笛卡尔坐标绘图缺失扇区问题求助

极坐标转笛卡尔坐标后等高线图扇区缺失问题解决

问题背景

我在外部软件中以圆柱坐标R、phi、z完成测量,固定z值后基于R和phi绘制等高线图。通过以下代码将极坐标转换为笛卡尔坐标x、y:

t_xray = np.zeros((Rbins, Phibins))
t_yray = np.zeros((Rbins, Phibins))

for i in range(0, Rbins):
    for j in range(0, Phibins):
        t_xray[i,j] = Rray[i] * np.cos(Phiray[j])
        t_yray[i,j] = Rray[i] * np.sin(Phiray[j])

其中Rbins和Phibins分别为R数组与phi数组的长度。随后使用以下代码绘制等高线图:

plt.contourf(t_xray, t_yray, Doos_TG43, 1000, locator = ticker.LogLocator())
cbar = plt.colorbar(label = r'$\it{D}$ (cGy$\cdot$ h$^{-1}$)')

但结果图出现扇区缺失。最初怀疑是phi角度间隔不均,但打印phi数组发现首尾及相邻元素间隔一致:

[0.03141593 0.09424778 0.15707963 0.21991149 0.28274334 0.34557519
 0.40840704 0.4712389  0.53407075 0.5969026  0.65973446 0.72256631
 0.78539816 0.84823002 0.91106187 0.97389372 1.03672558 1.09955743
 1.16238928 1.22522113 1.28805299 1.35088484 1.41371669 1.47654855
 1.5393804  1.60221225 1.66504411 1.72787596 1.79070781 1.85353967
 1.91637152 1.97920337 2.04203522 2.10486708 2.16769893 2.23053078
 2.29336264 2.35619449 2.41902634 2.4818582  2.54469005 2.6075219
 2.67035376 2.73318561 2.79601746 2.85884931 2.92168117 2.98451302
 3.04734487 3.11017673 3.17300858 3.23584043 3.29867229 3.36150414
 3.42433599 3.48716785 3.5499997  3.61283155 3.6756634  3.73849526
 3.80132711 3.86415896 3.92699082 3.98982267 4.05265452 4.11548638
 4.17831823 4.24115008 4.30398194 4.36681379 4.42964564 4.49247749
 4.55530935 4.6181412  4.68097305 4.74380491 4.80663676 4.86946861
 4.93230047 4.99513232 5.05796417 5.12079603 5.18362788 5.24645973
 5.30929158 5.37212344 5.43495529 5.49778714 5.560619   5.62345085
 5.6862827  5.74911456 5.81194641 5.87477826 5.93761012 6.00044197
 6.06327382 6.12610567 6.18893753 6.25176938]

排查角度间隔后仍未找到原因,需要解决绘图扇区缺失问题。

问题原因与解决方案

1. 核心原因:角度范围未闭合

你的phi数组最后一个值约为6.2517,接近2π(≈6.2832)但并未完全闭合到0或2π。contourf处理极坐标转笛卡尔的网格时,需要角度范围形成闭环(起始点与终点重合),否则会出现扇区缺口。

2. 修复方案

方案一:补全角度数组,强制闭合

在phi数组末尾添加2*np.pi,同时复制Doos_TG43的第一列作为最后一列(因为角度0和2π对应同一位置),确保网格闭环:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# 补全角度到2π
Phiray = np.append(Phiray, 2 * np.pi)
# 复制第一列数据作为最后一列,保持数据连续性
Doos_TG43 = np.hstack((Doos_TG43, Doos_TG43[:, [0]]))

# 重新计算x、y网格(用广播替代循环,提升效率)
Rbins, Phibins = len(Rray), len(Phiray)
R_grid, Phi_grid = np.meshgrid(Rray, Phiray, indexing='ij')
t_xray = R_grid * np.cos(Phi_grid)
t_yray = R_grid * np.sin(Phi_grid)

# 重新绘图
plt.contourf(t_xray, t_yray, Doos_TG43, 1000, locator=ticker.LogLocator())
cbar = plt.colorbar(label=r'$\it{D}$ (cGy$\cdot$ h$^{-1}$)')
plt.show()

方案二:直接使用极坐标绘图(更简便)

无需转换笛卡尔坐标,切换到matplotlib的极坐标投影模式,从根源避免闭合问题:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

# 创建极坐标网格
R_grid, Phi_grid = np.meshgrid(Rray, Phiray)
# 切换到极坐标子图
plt.subplot(projection='polar')
# 注意转置Doos_TG43,匹配meshgrid的维度顺序
plt.contourf(Phi_grid, R_grid, Doos_TG43.T, 1000, locator=ticker.LogLocator())
cbar = plt.colorbar(label=r'$\it{D}$ (cGy$\cdot$ h$^{-1}$)')
plt.show()

3. 额外优化建议

  • 用numpy广播替代嵌套循环,大幅提升网格计算效率(如方案一中的meshgrid用法);
  • 确保R数组包含0值(如果需要绘制中心区域),避免出现中心空洞。

内容的提问来源于stack exchange,提问作者Aron Koppe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:40:34