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

使用Matplotlib绘制半极坐标图的异常问题排查

问题原因及解决方法

核心问题:角度单位不匹配

Matplotlib极坐标的theta参数默认使用弧度,但你传入的angle是角度值(-90°到90°),这直接导致点的位置完全错误。

修正步骤:

  • 将角度转换为弧度:使用np.deg2rad()把角度数组转成弧度,这是最关键的一步。
  • 移除无效的xlim设置:极坐标下用set_thetamin()/set_thetamax()已经足够限制角度范围,xlim在这里不生效。
  • (可选)调整径向轴方向:如果目标图径向轴是从外到内递增(比如中心为100、边缘为0),可额外反转径向轴。

修正后的代码:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.set_thetamin(-90)
ax.set_thetamax(90)
ax.set_theta_offset(.5 * np.pi)
ax.set_thetagrids(range(-90, 100, 15))

# 关键:将角度转换为弧度
angle_rad = np.deg2rad(np.linspace(-90, 90, 19))
valeurs = np.array([4.9,6.5,11,29.1,44.9,57.2,76,87.7,97.2,100,95,83,68,48.1,31.1,19.7,9,5.8,5.3])   

plt.plot(angle_rad, valeurs, "o ")
plt.show()

额外适配(若需匹配特定目标图):

如果目标图的径向轴是从外到内数值递增,添加以下代码反转径向轴:

ax.set_ylim(100, 0)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 03:20:25