Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: clockwise polar plot

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?

like image 737
kame Avatar asked Oct 05 '11 15:10

kame


2 Answers

Add these lines:

ax.set_theta_direction(-1)
ax.set_theta_offset(pi/2.0)
like image 157
Pavel Avatar answered Sep 20 '22 20:09

Pavel


ax.set_theta_direction(-1)
ax.set_theta_zero_location('N')

is slightly more comprehensible.

like image 42
gbronner Avatar answered Sep 20 '22 20:09

gbronner