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

Matplotlib极坐标图:如何设置theta轴零点位置并反转其递增方向?

嘿,刚好对Matplotlib极坐标的轴设置比较熟悉,这几个需求其实都能通过简单的API调用搞定,我给你一步步拆解:

1. 指定Theta轴的零点位置

Matplotlib提供了set_theta_zero_location()方法来自定义theta轴的零点位置,你可以传入方向字符串或者具体角度值:

  • 方向参数:'n'(北,顶部)、'e'(东,默认右侧)、's'(南,底部)、'w'(西,左侧)
  • 角度值:直接传入度数,比如90等价于'n'(因为默认东是0度,逆时针转90度就是顶部)

示例代码:

import matplotlib.pyplot as plt
import numpy as np

# 生成测试数据
theta = np.linspace(0, 2*np.pi, 100)
r = np.sin(2*theta)

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)

# 将零点设置在顶部(北)
ax.set_theta_zero_location('n')  # 也可以写成ax.set_theta_zero_location(90)

plt.show()
2. 反转Theta轴的角度递增方向

默认角度是逆时针递增,要改成顺时针的话,调用set_theta_direction()方法,传入参数1即可(默认参数是-1,对应逆时针)。

示例代码:

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)

# 切换为顺时针递增
ax.set_theta_direction(1)

plt.show()
3. 同时设置零点位置+反转角度方向

把上面两个方法结合起来就能实现组合需求,比如让零点在顶部,角度顺时针递增:

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)

# 零点在顶部 + 角度顺时针递增
ax.set_theta_zero_location('n')
ax.set_theta_direction(1)

plt.show()
4. 反转径向(r)轴

如果需要反转径向轴(让大的r值靠近中心,小的r值在外围),可以直接调用invert_yaxis()方法(极坐标的r轴对应Matplotlib的y轴):

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)

# 反转r轴方向
ax.invert_yaxis()

plt.show()

内容的提问来源于stack exchange,提问作者Steven C. Howell

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:09:41