Python 如何使matplotlib极坐标图中的角度以0°顺时针旋转;在顶端?

Python 如何使matplotlib极坐标图中的角度以0°顺时针旋转;在顶端?,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我正在使用matplotlib和numpy制作极坐标图。以下是一些示例代码: import numpy as N import matplotlib.pyplot as P angle = N.arange(0, 360, 10, dtype=float) * N.pi / 180.0 arbitrary_data = N.abs(N.sin(angle)) + 0.1 * (N.random.random_sample(size=angle.shape) - 0.5) P.clf() P.

我正在使用matplotlib和numpy制作极坐标图。以下是一些示例代码:

import numpy as N
import matplotlib.pyplot as P

angle = N.arange(0, 360, 10, dtype=float) * N.pi / 180.0
arbitrary_data = N.abs(N.sin(angle)) + 0.1 * (N.random.random_sample(size=angle.shape) - 0.5)

P.clf()
P.polar(angle, arbitrary_data)
P.show()

您会注意到0°位于绘图上的3点钟处,并且角度为逆时针方向。对于我的数据可视化目的来说,在12点时为0°并且角度为顺时针方向更有用。除了旋转数据和手动更改轴标签外,还有其他方法可以做到这一点吗?

您可以修改matplotlib/projections/polar.py

上面写着:

def transform(self, tr):
        xy   = npy.zeros(tr.shape, npy.float_)
        t    = tr[:, 0:1]
        r    = tr[:, 1:2]
        x    = xy[:, 0:1]
        y    = xy[:, 1:2]
        x[:] = r * npy.cos(t)
        y[:] = r * npy.sin(t)
        return xy
让它说:

def transform(self, tr):
        xy   = npy.zeros(tr.shape, npy.float_)
        t    = tr[:, 0:1]
        r    = tr[:, 1:2]
        x    = xy[:, 0:1]
        y    = xy[:, 1:2]
        x[:] = - r * npy.sin(t)
        y[:] = r * npy.cos(t)
        return xy
实际上我没有尝试过,您可能需要根据自己的喜好调整x[:]和y[:]分配。此更改将影响所有使用matplotlib极坐标图的程序。

我发现了它——matplotlib允许您创建自定义投影。我创建了一个继承自PolarAxes的

将numpy导入为np
将matplotlib.pyplot作为plt导入
从matplotlib.projections导入极坐标,注册投影
从matplotlib.transforms导入仿射2D、Bbox、IdentityTransform
北极类(北极类):
'''
θ开始指向北方并移动的一种极轴变体
顺时针方向的
'''
名称='northpolar'
类别NorthPolarTransform(PolarAxes.PolarTransform):
def转换(自我,tr):
xy=np.零(tr.形,np.浮点数)
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)
返回xy
变换非仿射=变换
def倒置(自身):
return NorthPolarAxes.InvertedNorthPolarTransform()
类逆变器北极变换(PolarAxes.InvertedPolarTransform):
def变换(自,xy):
x=xy[:,0:1]
y=xy[:,1:]
r=np.sqrt(x*x+y*y)
θ=np.arctan2(y,x)
返回np.concatenate((θ,r),1)
def倒置(自身):
返回NorthPolarAxes.NorthPolarTransform()
定义集和变换(自):
PolarAxes._集_lim_和_变换(自)
self.transProjection=self.NorthPolarTransform()
self.transData=(self.transScale+self.transProjection+(self.transProjectionAffine+self.transAxes))
self._xaxis_transform=(self.transProjection+self.polarafine(IdentityTransform(),Bbox.unit())+self.transAxes)
self.\u xaxis\u text1\u变换=(self.\u theta\u label1\u位置+self.\u xaxis\u变换)
self._yaxis_transform=(Affine2D().scale(np.pi*2.0,1.0)+self.transData)
self.\u yaxis\u text1\u变换=(self.\r\u label1\u位置+仿射2D().比例(1.0/360.0,1.0)+self.\u yaxis\u变换)
寄存器_投影(NorthPolarAxes)
角度=np.arange(0,360,10,数据类型=浮点)*np.pi/180.0
任意_数据=(np.abs(np.sin(角度))+0.1*
(np.随机.随机样本(大小=角度形状)-0.5)
plt.clf()
plt.子地块(1,1,1,投影='northpolar')
plt.绘图(角度、任意_数据)
plt.show()

两个反转例程都应使用转换的完整路径:

return NorthPolarAxes.InvertedNorthPolarTransform()

现在,自动创建的NorthPolarAxesSubplot等NorthPolararAXES子类可以访问转换函数


希望这有帮助。

在Matplotlib 1.1中更新此问题,现在在
PolarAxes
中有两种方法用于设置θ方向(CW/CCW)和θ=0的位置

退房

具体请参见
set theta\u direction()
set theta\u offset()

很多人似乎都在尝试做类似指南针的绘图。

要展开,请举一个例子:

from math import radians
import matplotlib.pyplot as plt

angle=[0.,5.,10.,15.,20.,25.,30.,35.,40.,45.,50.,55.,60.,65.,70.,75.,\
       80.,85.,90.,95.,100.,105.,110.,115.,120.,125.]

angle = [radians(a) for a in angle]

lux=[12.67,12.97,12.49,14.58,12.46,12.59,11.26,10.71,17.74,25.95,\
     15.07,7.43,6.30,6.39,7.70,9.19,11.30,13.30,14.07,15.92,14.70,\
     10.70,6.27,2.69,1.29,0.81]

plt.clf()
sp = plt.subplot(1, 1, 1, projection='polar')
sp.set_theta_zero_location('N')
sp.set_theta_direction(-1)
plt.plot(angle, lux)
plt.show()


这很巧妙,但修补代码是一种欺骗,不是吗?然而,你给了我一个想法。Matplotlib允许您使用任何类型的变换创建轴;也许我可以用我正在寻找的转换编写一个替代的polar()函数。太棒了,这应该包括在他们的自定义投影示例中。@ptomato-我想答案需要更新2012-01-23(git log-Sself.\r\u label1\u位置)的提交。不确定最终是哪个版本。在任何情况下,都不会有self。_r_label1_位置,删除该行似乎可以解决问题。在这种情况下,应该更新它以匹配
PolarAxes
现在的样子…如果你有一个极轴ax:ax.set_theta_offset(0.5*numpy.pi)
from math import radians
import matplotlib.pyplot as plt

angle=[0.,5.,10.,15.,20.,25.,30.,35.,40.,45.,50.,55.,60.,65.,70.,75.,\
       80.,85.,90.,95.,100.,105.,110.,115.,120.,125.]

angle = [radians(a) for a in angle]

lux=[12.67,12.97,12.49,14.58,12.46,12.59,11.26,10.71,17.74,25.95,\
     15.07,7.43,6.30,6.39,7.70,9.19,11.30,13.30,14.07,15.92,14.70,\
     10.70,6.27,2.69,1.29,0.81]

plt.clf()
sp = plt.subplot(1, 1, 1, projection='polar')
sp.set_theta_zero_location('N')
sp.set_theta_direction(-1)
plt.plot(angle, lux)
plt.show()