2010-03-10 14 views
26

मैं ध्रुवीय साजिश बनाने के लिए matplotlib और numpy का उपयोग कर रहा हूँ। यहां कुछ नमूना कोड है:matplotlib ध्रुवीय साजिश में कोण कैसे बनाने के लिए शीर्ष पर 0 डिग्री के साथ घड़ी की दिशा में जाओ?

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 डिग्री रखने के लिए और अधिक कोणों के लिए घड़ी के विपरीत होने के लिए और अधिक उपयोगी होगा। के अलावा डेटा घुमाने और अक्ष अक्षरों को मैन्युअल रूप से बदलने के अलावा कोई तरीका है?

उत्तर

18

मुझे यह पता चला - 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() 
+1

बहुत बढ़िया, इसे अपने कस्टम प्रोजेक्शन उदाहरणों के साथ शामिल किया जाना चाहिए। – Mark

+1

मैं अनुशंसा करता हूं कि इसे कोडबेस में शामिल किया जाए। –

+0

कोई विचार यह है कि इसे FigCanvasGTKAgg के साथ कैसे काम करना है? – Sardathrion

4

आप अपने matplotlib/अनुमान/polar.py को संशोधित कर सकते हैं।

यह कहाँ का कहना है: और y: करने के लिए कार्य:

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 

मैं वास्तव में यह कोशिश नहीं की है, तो आप बदलाव करने की आवश्यकता हो सकती एक्स [:] []

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 

यह कहना बनाओ आपका स्वाद। यह परिवर्तन matplotlib ध्रुवीय साजिश का उपयोग करने वाले सभी कार्यक्रमों को प्रभावित करेगा।

+0

यह सरल है, लेकिन कोड पैचिंग धोखाधड़ी का प्रकार है, है ना? हालांकि, आपने मुझे एक विचार दिया है। Matplotlib आपको किसी भी प्रकार के परिवर्तन के साथ अक्ष बनाने की अनुमति देता है; शायद मैं एक वैकल्पिक ध्रुवीय() फ़ंक्शन को उस ट्रांसफॉर्म के साथ लिख सकता हूं जिसे मैं ढूंढ रहा हूं। – ptomato

2

दोनों उलट दिनचर्या रूपांतरण का पूरा पथ का उपयोग करना चाहिए:

return NorthPolarAxes.InvertedNorthPolarTransform() 

और

return NorthPolarAxes.NorthPolarTransform() 

अब, इस तरह के रूप में NorthPolarAxesSubplot NorthPolarAxes के स्वचालित रूप से बनाया उपवर्गों को बदलने कार्यों पहुँच सकते हैं।

उम्मीद है कि इससे मदद मिलती है।

28

Matplotlib 1.1 में, इस प्रश्न को अद्यतन करते हुए, थेटा दिशा (सीडब्ल्यू/सीसीडब्ल्यू) और theta = 0 के लिए स्थान सेट करने के लिए PolarAxes में दो विधियां हैं।

चेक बाहर http://matplotlib.sourceforge.net/devel/add_new_projection.html#matplotlib.projections.polar.PolarAxes

विशेष रूप से, set_theta_direction() और set_theta_offset() देखते हैं।

बहुत से लोग कंपास की तरह प्लॉट करने की कोशिश कर रहे हैं।

+1

यदि आपके पास ध्रुवीय अक्ष * कुल्हाड़ी * है: ** ax.set_theta_offset (0.5 * numpy.pi) ** – zinjaai

14

एक उदाहरण के साथ 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() 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^