我正在使用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°并且角度顺时针方向将更有用。有没有办法做到这一点除了旋转数据和手动更改轴标签?
答案 0 :(得分:29)
更新这个问题,在Matplotlib 1.1中,PolarAxes
中有两种方法用于设置theta方向(CW / CCW)和theta = 0的位置。
具体来说,请参阅set_theta_direction()
和set_theta_offset()
。
很多人试图做类似罗盘的情节。
答案 1 :(得分:20)
我发现了 - matplotlib允许您创建自定义投影。我创建了一个继承自PolarAxes
。
import numpy as N
import matplotlib.pyplot as P
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 = N.zeros(tr.shape, N.float_)
t = tr[:, 0:1]
r = tr[:, 1:2]
x = xy[:, 0:1]
y = xy[:, 1:2]
x[:] = r * N.sin(t)
y[:] = r * N.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 = N.sqrt(x*x + y*y)
theta = N.arctan2(y, x)
return N.concatenate((theta, r), 1)
def inverted(self):
return NorthPolarAxes.NorthPolarTransform()
def _set_lim_and_transforms(self):
PolarAxes._set_lim_and_transforms(self)
self.transProjection = self.NorthPolarTransform()
self.transData = (
self.transScale +
self.transProjection +
(self.transProjectionAffine + self.transAxes))
self._xaxis_transform = (
self.transProjection +
self.PolarAffine(IdentityTransform(), Bbox.unit()) +
self.transAxes)
self._xaxis_text1_transform = (
self._theta_label1_position +
self._xaxis_transform)
self._yaxis_transform = (
Affine2D().scale(N.pi * 2.0, 1.0) +
self.transData)
self._yaxis_text1_transform = (
self._r_label1_position +
Affine2D().scale(1.0 / 360.0, 1.0) +
self._yaxis_transform)
register_projection(NorthPolarAxes)
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.subplot(1, 1, 1, projection='northpolar')
P.plot(angle, arbitrary_data)
P.show()
答案 2 :(得分:17)
用一个例子扩展klimaat的答案:
import math
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 = [math.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]
import matplotlib.pyplot as P
import matplotlib
P.clf()
sp = P.subplot(1, 1, 1, projection='polar')
sp.set_theta_zero_location('N')
sp.set_theta_direction(-1)
P.plot(angle, lux)
P.show()
答案 3 :(得分:4)
您可以修改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极坐标图的所有程序。
答案 4 :(得分:2)
两个反转例程都应该使用转换的完整路径:
return NorthPolarAxes.InvertedNorthPolarTransform()
和
return NorthPolarAxes.NorthPolarTransform()
现在,自动创建的NorthPolarAxes子类(如NorthPolarAxesSubplot)可以访问转换函数。
希望这有帮助。