如何在 Matplotlib 极坐标图中(楔形图)中设置 0 周围(两侧)的限制

发布于 2025-01-13 11:25:01 字数 975 浏览 3 评论 0原文

我正在制作一个楔形图(在太空中绘制类星体,RA 为 theta,Dec 为 r)。我需要在 0 的两侧设置极坐标图的限制。我的限制应该从 45 度到 315 度,这两个值之间有 0 度 (45-0-315)。我该怎么做?

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

plt.show()

如果我设置限制:

ax.set_thetamin(45)
ax.set_thetamax(-45)

我会得到图表的正确切片,但 theta 轴上的值错误(轴现在从 -45 到 45 度)。

如果我设置限制:

ax.set_thetamin(45)
ax.set_thetamax(315)

我会得到错误的图表切片,但在 theta 轴上得到正确的值。

该怎么办?

I am making a wedge diagram (plotting quasars in space, with RA as theta and Dec as r). I need to set the limits of a polar plot on both sides of 0. My limits should go from 45 degrees to 315 degrees with 0 degrees in between those two values (45-0-315). How do I do this?

This is my code:

import numpy as np
import matplotlib.pyplot as plt

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

plt.show()

If I put the limits:

ax.set_thetamin(45)
ax.set_thetamax(-45)

I get the correct slice of the diagram, but the wrong values on the theta axis (the axis now goes from -45-45 degrees).

If I put the limits:

ax.set_thetamin(45)
ax.set_thetamax(315)

I get the wrong slice of the diagram, but the correct values on the theta axis.

What to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2025-01-20 11:25:01

看起来,如果 thetaminthetamax 具有正值和负值,matplotlib 只会使 theta 限制跨越 theta=0。来自 set_thetalim()

值包含在 [0, 2π] 范围内(以弧度为单位),因此例如可以执行 set_thetalim(-np.pi / 2, np.pi / 2) 以获得围绕 0 对称的轴.

因此设置:

ax.set_thetamin(45)
ax.set_thetamax(-45)

是获得您想要的情节的正确做法。然后我们可以稍后使用 ticker.FuncFormatter< /code>获取您想要的刻度值。

例如:

import matplotlib.ticker as ticker

fmt = lambda x, pos: "{:g}".format(np.degrees(x if x >= 0 else x + 2 * np.pi))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt))

产生:

在此处输入图像描述

为了完整起见,我将其全部放在您的脚本中:

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

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

ax.set_thetamin(45)
ax.set_thetamax(-45)

fmt = lambda x, pos: "{:g}".format(np.degrees(x if x >= 0 else x + 2 * np.pi))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt))

plt.show()

It appears that matplotlib will only make the theta limits span across theta=0 if you have a positive and negative value for thetamin and thetamax. From the docstring for set_thetalim():

Values are wrapped in to the range [0, 2π] (in radians), so for example it is possible to do set_thetalim(-np.pi / 2, np.pi / 2) to have an axes symmetric around 0.

So setting:

ax.set_thetamin(45)
ax.set_thetamax(-45)

is the correct thing to do to get the plot you want. We can then modify the ticks later using a ticker.FuncFormatter to get the tick values you want.

For example:

import matplotlib.ticker as ticker

fmt = lambda x, pos: "{:g}".format(np.degrees(x if x >= 0 else x + 2 * np.pi))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt))

Which yields:

enter image description here

For completeness, here I put it all together in your script:

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

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

ax.set_thetamin(45)
ax.set_thetamax(-45)

fmt = lambda x, pos: "{:g}".format(np.degrees(x if x >= 0 else x + 2 * np.pi))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt))

plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文