如何在 Matplotlib 极坐标图中(楔形图)中设置 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来,如果
thetamin
和thetamax
具有正值和负值,matplotlib 只会使 theta 限制跨越 theta=0。来自set_thetalim()
:
因此设置:
是获得您想要的情节的正确做法。然后我们可以稍后使用
ticker.FuncFormatter< /code>
获取您想要的刻度值。
例如:
产生:
为了完整起见,我将其全部放在您的脚本中:
It appears that matplotlib will only make the theta limits span across theta=0 if you have a positive and negative value for
thetamin
andthetamax
. From the docstring forset_thetalim()
:So setting:
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:
Which yields:
For completeness, here I put it all together in your script: