如何使极坐标图中的角度以 0° 为顺时针方向在顶部

发布于 2024-08-25 03:47:51 字数 446 浏览 5 评论 0原文

我正在使用 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°,并且角度为顺时针方向,则会更有用。除了旋转数据和手动更改轴标签之外,还有什么方法可以做到这一点吗?

I am using matplotlib and numpy to make a polar plot. Here is some sample code:

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()

You will notice that 0° is at 3 o'clock on the plot, and the angles go counterclockwise. It would be more useful for my data visualization purposes to have 0° at 12 o'clock and have the angles go clockwise. Is there any way to do this besides rotating the data and manually changing the axis labels?

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

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

发布评论

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

评论(5

不甘平庸 2024-09-01 03:47:51

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

查看
http://matplotlib.sourceforge.net/devel/add_new_projection.html #matplotlib.projections.polar.PolarAxes

具体请参阅 set_theta_direction()set_theta_offset()

看来很多人都在尝试做类似指南针的情节。

Updating this question, in Matplotlib 1.1, there are now two methods in PolarAxes for setting the theta direction (CW/CCW) and location for theta=0.

Check out
http://matplotlib.sourceforge.net/devel/add_new_projection.html#matplotlib.projections.polar.PolarAxes

Specifically, see set_theta_direction() and set_theta_offset().

Lots of people attempting to do compass-like plots it seems.

一枫情书 2024-09-01 03:47:51

展开klimaat答案 ,举例:

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()

在此处输入图像描述

To expand klimaat's answer, with an example:

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()

enter image description here

茶色山野 2024-09-01 03:47:51

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

import numpy as np
import matplotlib.pyplot as plt

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)
            theta = np.arctan2(y, x)
            return np.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(np.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 = np.arange(0, 360, 10, dtype=float) * np.pi / 180.0
arbitrary_data = (np.abs(np.sin(angle)) + 0.1 * 
    (np.random.random_sample(size=angle.shape) - 0.5))

plt.clf()
plt.subplot(1, 1, 1, projection='northpolar')
plt.plot(angle, arbitrary_data)
plt.show()

输入图片此处描述

I found it out -- matplotlib allows you to create custom projections. I created one that inherits from PolarAxes.

import numpy as np
import matplotlib.pyplot as plt

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)
            theta = np.arctan2(y, x)
            return np.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(np.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 = np.arange(0, 360, 10, dtype=float) * np.pi / 180.0
arbitrary_data = (np.abs(np.sin(angle)) + 0.1 * 
    (np.random.random_sample(size=angle.shape) - 0.5))

plt.clf()
plt.subplot(1, 1, 1, projection='northpolar')
plt.plot(angle, arbitrary_data)
plt.show()

enter image description here

十雾 2024-09-01 03:47:51

您可以修改 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 极坐标图的程序。

You could modify your matplotlib/projections/polar.py.

Where it says:

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

Make it say:

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

I didn't actually try it, you may need to tweak x[:] and y[:] assignments to your taste. This change will affect all programs that use matplotlib polar plot.

雨夜星沙 2024-09-01 03:47:51

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

return NorthPolarAxes.InvertedNorthPolarTransform()

现在

return NorthPolarAxes.NorthPolarTransform()

,自动创建的 NorthPolarAxes 子类(例如 NorthPolarAxesSubplot)可以访问变换函数。

希望这有帮助。

Both invert routines should use the full path to the transforms:

return NorthPolarAxes.InvertedNorthPolarTransform()

and

return NorthPolarAxes.NorthPolarTransform()

Now, automatically created subclasses of NorthPolarAxes such as NorthPolarAxesSubplot can access the transform functions.

Hope this helps.

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