2017-02-22 17 views
2

私はこの機能をycopin - GitHubから変更しました。Pythonで軸を逆に傾ける

#!/usr/bin/env python 
# Copyright: This document has been placed in the public domain. 

""" 
Taylor diagram (Taylor, 2001) test implementation. 
http://www-pcmdi.llnl.gov/about/staff/Taylor/CV/Taylor_diagram_primer.htm 
""" 

__version__ = "Time-stamp: <2012-02-17 20:59:35 ycopin>" 
__author__ = "Yannick Copin <[email protected]>" 

import numpy as NP 
import matplotlib.pyplot as PLT 

class TaylorDiagram(object): 
    """Taylor diagram: plot model standard deviation and correlation 
    to reference (data) sample in a single-quadrant polar plot, with 
    r=stddev and theta=arccos(correlation). 
    """ 

    def __init__(self, refstd, fig=None, rect=111, label='_'): 
     """Set up Taylor diagram axes, i.e. single quadrant polar 
     plot, using mpl_toolkits.axisartist.floating_axes. refstd is 
     the reference standard deviation to be compared to. 
     """ 

     from matplotlib.projections import PolarAxes 
     import mpl_toolkits.axisartist.floating_axes as FA 
     import mpl_toolkits.axisartist.grid_finder as GF 

     self.refstd = refstd   # Reference standard deviation 

     tr = PolarAxes.PolarTransform() 

     # Correlation positive labels 
     rlocs = NP.concatenate(([-0.99,-0.9],NP.arange(-0.8,0,0.2), 
           NP.arange(0,0.9,0.2),[0.9,0.99])) 
     tlocs = NP.arccos(rlocs)  # Conversion to polar angles 
     gl1 = GF.FixedLocator(tlocs) # Positions 
     tf1 = GF.DictFormatter(dict(zip(tlocs, map(str,rlocs)))) 

     # Standard deviation axis extent 
     self.smin = 0 
     self.smax = 2*self.refstd/self.refstd 

     ghelper = FA.GridHelperCurveLinear(tr, 
              extremes=(0,NP.pi, # 1st quadrant 
                self.smin,self.smax), 
              grid_locator1=gl1, 
              tick_formatter1=tf1, 
              )       


     if fig is None: 
      fig = PLT.figure() 

     ax = FA.FloatingSubplot(fig, rect, grid_helper=ghelper) 
     fig.add_subplot(ax) 

     # Adjust axes 
     ax.axis["top"].set_axis_direction("bottom") # "Angle axis" 
     ax.axis["top"].toggle(ticklabels=True, label=True) 
     ax.axis["top"].major_ticklabels.set_axis_direction("top") 
     ax.axis["top"].label.set_axis_direction("top") 
     ax.axis["top"].label.set_text("Correlation") 


     ax.axis["left"].set_axis_direction("right") # "X axis" 
     ax.axis["right"].toggle(ticklabels=True) 
     ax.axis["right"].major_ticklabels.set_axis_direction("bottom") 

     #ax.axis["bottom"].label.set_text("Standard deviation") 

     ax.axis["right"].set_axis_direction("left") # "Y axis" 
     #ax.axis["right"].toggle(ticklabels=True) 
     ax.axis["right"].major_ticklabels.set_axis_direction("right") 

     ax.axis["bottom"].set_visible(False)   # Useless 

     # Contours along standard deviations 
     ax.grid(False) 

     self._ax = ax     # Graphical axes 
     self.ax = ax.get_aux_axes(tr) # Polar coordinates 

     # Add reference point and stddev contour 
     print "Reference std:", self.refstd/self.refstd 
     l, = self.ax.plot([0], self.refstd/self.refstd, 'k*', 
          ls='', ms=10, label=label) 
     t = NP.linspace(0, NP.pi) 
     r = NP.zeros_like(t) + self.refstd/self.refstd 
     self.ax.plot(t,r, 'k--', label='_') 

     # Collect sample points for latter use (e.g. legend) 
     self.samplePoints = [l] 

    def add_sample(self, stddev, corrcoef, *args, **kwargs): 
     """Add sample (stddev,corrcoeff) to the Taylor diagram. args 
     and kwargs are directly propagated to the Figure.plot 
     command.""" 

     l, = self.ax.plot(NP.arccos(corrcoef), stddev/self.refstd, 
          *args, **kwargs) # (theta,radius) 
     self.samplePoints.append(l) 

     return l 

    def add_contours(self, levels=5, **kwargs): 
     """Add constant centered RMS difference contours.""" 

     rs,ts = NP.meshgrid(NP.linspace(self.smin,self.smax), 
          NP.linspace(0,NP.pi)) 
     # Compute centered RMS difference 
     rms = NP.sqrt((self.refstd/self.refstd)**2 + rs**2 - 2*(self.refstd/self.refstd)*rs*NP.cos(ts)) 

     contours = self.ax.contour(ts, rs, rms, levels, **kwargs) 

     return contours 


if __name__=='__main__': 

    # Reference dataset 
    x = NP.linspace(0,4*NP.pi,100) 
    data = NP.sin(x) 
    refstd = data.std(ddof=1)   # Reference standard deviation 

    # Models 
    m1 = data + 0.2*NP.random.randn(len(x)) # Model 1 
    m2 = 0.8*data + .1*NP.random.randn(len(x)) # Model 2 
    m3 = NP.sin(x-NP.pi/10)     # Model 3 

    # Compute stddev and correlation coefficient of models 
    samples = NP.array([ [m.std(ddof=1), NP.corrcoef(data, m)[0,1]] 
         for m in (m1,m2,m3)]) 

    fig = PLT.figure(figsize=(10,4)) 

    ax1 = fig.add_subplot(1,2,1, xlabel='X', ylabel='Y') 
    # Taylor diagram 
    dia = TaylorDiagram(refstd, fig=fig, rect=122, label="Reference") 

    colors = PLT.matplotlib.cm.jet(NP.linspace(0,1,len(samples))) 

    ax1.plot(x,data,'ko', label='Data') 
    for i,m in enumerate([m1,m2,m3]): 
     ax1.plot(x,m, c=colors[i], label='Model %d' % (i+1)) 
    ax1.legend(numpoints=1, prop=dict(size='small'), loc='best') 

    # Add samples to Taylor diagram 
    for i,(stddev,corrcoef) in enumerate(samples): 
     dia.add_sample(stddev, corrcoef, marker='s', ls='', c=colors[i], 
         label="Model %d" % (i+1)) 

    # Add RMS contours, and label them 
    contours = dia.add_contours(colors='0.5') 
    PLT.clabel(contours, inline=1, fontsize=10) 

    # Add a figure legend 
    fig.legend(dia.samplePoints, 
       [ p.get_label() for p in dia.samplePoints ], 
       numpoints=1, prop=dict(size='small'), loc='upper right') 


    PLT.show() 

それが実行されると、それはダニ私が欲しいこのFIG1

fig1

は数x、yの(極軸)を低減することであることを示している...多分5(5、含みます参照データ)をそれぞれの標準偏差軸に置き、それらを水平に並べます。何か案が?

EDIT:これまで上記の図から

パス:私は、現時点では自分自身でこれをテストする方法はありません

+0

達成しようとしていることがわかりません。あなたはそれを他の方法のように見せたいもののあなたの説明を入れてもいいですか? –

+0

私の編集を参照してください...それは私が望むものです – Erincon

答えて

0

fig2

ティックの数を減らすことが、ベースTaylorDiagram.__init__内部

this linkのオフに、それはthereof--がうまくいくかもしれない次、-ORいくつかのバリエーションを思え

 ax.axis["right"].major_ticklabels.set_axis_direction("right") 

この行(または多分同様のもの)がこれに変更する必要があります。私はちょうど今のドキュメントを熟読することにより把握するためにダニやものの数については

 ax.axis["right"].major_ticklabels.set_axis_direction("bottom") 

、それは難しいですhelp(ax.axis["right"].major_ticklabels)を実行し、目盛りのラベルが何であるかを確認してください。おそらくあなたが手で編集したり設定したりできるリストがあります。

関連する問題