顶部 0 度的顺时针极坐标图
如何制作顺时针极坐标图?有人问类似的问题此处:如何使 matplotlib 极坐标图中的角度顺时针旋转,顶部为 0°?,但我不明白这一点:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.grid(True)
theta = np.arange(0,370,10)
theta = [i*np.pi/180.0 for i in theta] # convert to radians
x = [3.00001,3,3,3,3,3,3,3,3,3,3,3,3,3,2.5,2,2,2,2,2,1.5,1.5,1,1.5,2,2,2.5,2.5,3,3,3,3,3,3,3,3,3]
ax.plot(theta, x)
plt.show()
编辑:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.projections import PolarAxes, register_projection
from matplotlib.transforms import Affine2D, Bbox, IdentityTransform
class NorthPolarAxes(PolarAxes):
'''
A variant of PolarAxes where theta starts pointing north and goes
clockwise.
'''
name = 'northpolar'
class NorthPolarTransform(PolarAxes.PolarTransform):
def transform(self, tr):
xy = np.zeros(tr.shape, np.float_)
t = tr[:, 0:1]
r = tr[:, 1:2]
x = xy[:, 0:1]
y = xy[:, 1:2]
x[:] = r * np.sin(t)
y[:] = r * np.cos(t)
return xy
transform_non_affine = transform
def inverted(self):
return NorthPolarAxes.InvertedNorthPolarTransform()
class InvertedNorthPolarTransform(PolarAxes.InvertedPolarTransform):
def transform(self, xy):
x = xy[:, 0:1]
y = xy[:, 1:]
r = np.sqrt(x*x + y*y)
fig = plt.figure()
register_projection(NorthPolarAxes)
ax=plt.subplot(1, 1, 1, projection='northpolar')
theta=np.linspace(0,2*np.pi,37)
x = [3.00001,3,3,3,3,3,3,3,3,3,3,3,3,3,2.5,2,2,2,2,
2,1.5,1.5,1,1.5,2,2,2.5,2.5,3,3,3,3,3,3,3,3,3]
ax.plot(theta, x)
plt.show()
如何使用 register_projection(NorthPolarAxes)
正确吗?
How can I make a clockwise polar plot? Somebody ask a similar question here: How to make the angles in a matplotlib polar plot go clockwise with 0° at the top?, But I don't understand this:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.grid(True)
theta = np.arange(0,370,10)
theta = [i*np.pi/180.0 for i in theta] # convert to radians
x = [3.00001,3,3,3,3,3,3,3,3,3,3,3,3,3,2.5,2,2,2,2,2,1.5,1.5,1,1.5,2,2,2.5,2.5,3,3,3,3,3,3,3,3,3]
ax.plot(theta, x)
plt.show()
EDIT:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.projections import PolarAxes, register_projection
from matplotlib.transforms import Affine2D, Bbox, IdentityTransform
class NorthPolarAxes(PolarAxes):
'''
A variant of PolarAxes where theta starts pointing north and goes
clockwise.
'''
name = 'northpolar'
class NorthPolarTransform(PolarAxes.PolarTransform):
def transform(self, tr):
xy = np.zeros(tr.shape, np.float_)
t = tr[:, 0:1]
r = tr[:, 1:2]
x = xy[:, 0:1]
y = xy[:, 1:2]
x[:] = r * np.sin(t)
y[:] = r * np.cos(t)
return xy
transform_non_affine = transform
def inverted(self):
return NorthPolarAxes.InvertedNorthPolarTransform()
class InvertedNorthPolarTransform(PolarAxes.InvertedPolarTransform):
def transform(self, xy):
x = xy[:, 0:1]
y = xy[:, 1:]
r = np.sqrt(x*x + y*y)
fig = plt.figure()
register_projection(NorthPolarAxes)
ax=plt.subplot(1, 1, 1, projection='northpolar')
theta=np.linspace(0,2*np.pi,37)
x = [3.00001,3,3,3,3,3,3,3,3,3,3,3,3,3,2.5,2,2,2,2,
2,1.5,1.5,1,1.5,2,2,2.5,2.5,3,3,3,3,3,3,3,3,3]
ax.plot(theta, x)
plt.show()
How to use register_projection(NorthPolarAxes)
correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加这些行:
Add these lines:
稍微更容易理解一些。
is slightly more comprehensible.
编辑:请注意,帕维尔提供了更好的解决方案!
您链接到的SO问题包含答案。这是 ptomato 的
NorthPolarAxes
类,具有theta=0
指向向东并顺时针递增:来自
matplotlib/projections/polar.py
的PolarTransform
和InvertedPolarTransform
是因为我认为它们有助于解释每个组件的作用。这将指导您更改公式。要获得顺时针方向的行为,您只需更改
t
-->-t
:在
InvertedEastPolarTransform
中,当y > 时,我们要使用
(上半平面),而不是当 y2 * npy.pi - theta
0y
时0 。
Edit: Please note that Pavel has provided a much better solution!
The SO question you linked to contains the answer. Here is a slightly modified version of ptomato's
NorthPolarAxes
class withtheta=0
pointing East and increasing clockwise:The doc strings from
matplotlib/projections/polar.py
'sPolarTransform
andInvertedPolarTransform
were added because I think they help explain what each component is doing. That guides you in changing the formulas.To get clockwise behavior, you simply change
t
-->-t
:and in
InvertedEastPolarTransform
, we want to use2 * npy.pi - theta
wheny > 0
(the upper half-plane) instead of wheny < 0
.简答
使用以下代码创建子图:
解释
使用
projection = 'polar'
时,创建的子图使用类matplotlib.projections.polar.PolarAxes 接受参数theta_offset
和theta_direction
。或者,您可以创建不带参数的极坐标子图,然后调用 ax.set_theta_zero_location` 和 ax.set_theta_direction 偏移 theta 轴并改变它的方向。
您可以查看其他方法,例如 set_thetalim
示例
Short answer
Create your subplot using this code:
Explanation
When using
projection = 'polar'
, the subplot created uses a projection of class matplotlib.projections.polar.PolarAxes which accepts parameterstheta_offset
andtheta_direction
.Alternatively you can create the polar subplot without parameters, and later call ax.set_theta_zero_location` and ax.set_theta_direction to offset theta axis and change its direction.
You may look at other methods like set_thetalim
Example