def setup_axes(fig, rect): """Polar projection, but in a rectangular box.""" # see demo_curvelinear_grid.py for details tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(12) grid_locator2 = grid_finder.MaxNLocator(5) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1) ax1 = fig.add_subplot(rect, axes_class=axisartist.Axes, grid_helper=grid_helper) ax1.axis[:].set_visible(False) ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) return ax1
def setup_axes(fig, rect): """ polar projection, but in a rectangular box. """ # 细节可以参考前面“曲线网格”的例子 tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(12) grid_locator2 = grid_finder.MaxNLocator(5) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1 ) ax1 = axisartist.Subplot(fig, rect, grid_helper=grid_helper) ax1.axis[:].toggle(ticklabels=False) fig.add_subplot(ax1) ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) return ax1
def __init__(self, refstd, fig=None, rect=111, label=None, cors=[0.3, 0.6, 0.9]): """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 as axisartist import mpl_toolkits.axisartist.grid_finder as GF self.refstd = refstd # Reference standard deviation tr = PolarAxes.PolarTransform() # Correlation labels rlocs = NP.concatenate((NP.arange(10) / 10., [0., 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.0 self.smax = 1.6 * self.refstd gl2 = GF.MaxNLocator(10) #[0, 0.2,0.4,0.6, 0.8,1.0,1.2,1.4] ghelper = FA.GridHelperCurveLinear( tr, extremes=( 0, NP.pi / 2.0, # 1st quadrant self.smin, self.smax), grid_locator1=gl1, grid_locator2=gl2, tick_formatter1=tf1, tick_formatter2=None, ) ghelper.grid_finder.grid_locator2._nbins = 4 if fig is None: fig = PLT.figure() ax = FA.FloatingSubplot(fig, rect, grid_helper=ghelper) fig.add_subplot(ax) # Adjust axes PLT.setp(ax.spines.values(), visible=False) 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["top"].label.set_fontsize(9) ax.axis[:].major_ticklabels.set_fontsize(12) ax.axis["left"].set_axis_direction("right") # "X axis" ax.axis["left"].toggle(ticklabels=True, label=True) ax.axis["left"].toggle(ticklabels=False, label=False) ax.axis["left"].label.set_text("Normalized std.") ax.axis["left"].label.set_fontsize(9) ax.axis["right"].set_axis_direction("top") # "Y axis" ax.axis["right"].toggle(ticklabels=True, label=True) ax.axis["right"].major_ticklabels.set_axis_direction("left") ax.axis["right"].label.set_fontsize(9) 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 l, = self.ax.plot([0], self.refstd, color='black', marker='o', ls='', ms=10, label="OBS") t = NP.linspace(0, NP.pi / 2.0) r = NP.zeros_like(t) + self.refstd lines = self.ax.plot(t, r, color='darkgray', ls='--', label='_', zorder=0, lw=0.8) for isec in cors: rad = NP.arccos(isec) stds = NP.linspace(self.smin, self.smax) rad0 = [rad for x in stds] lines = self.ax.plot(rad0, stds, color='darkgray', ls='--', zorder=0, lw=0.8) self.samplePoints = [l]
def taylor_ax(fig, *args, **kw): #th1=0., th2=np.pi/3, rd1=0., rd2=1.2, tloc1=None, tloc2=None) tr = PolarAxes.PolarTransform() # Correlation labels if kw.has_key("tloc1"): rlocs = kw["tloc1"] else: rlocs = np.concatenate((np.arange(10) / 10., [0.95, 0.99])) tlocs = np.arccos(rlocs) gl1 = GF.FixedLocator(tlocs) tf1 = GF.DictFormatter(dict(zip(tlocs, map(str, rlocs)))) if kw.has_key("tloc2"): gl2 = GF.FixedLocator(kw["tloc2"]) else: gl2 = GF.MaxNLocator(10) if kw.has_key("th1"): th1 = kw["th1"] else: th1 = 0. if kw.has_key("th2"): th2 = kw["th2"] else: th2 = np.pi / 2 if kw.has_key("rd1"): rd1 = kw["rd1"] else: rd1 = 0. if kw.has_key("rd2"): rd2 = kw["rd2"] else: rd2 = 1.65 ghelper = FA.GridHelperCurveLinear( tr, extremes=(th1, th2, rd1, rd2), grid_locator1=gl1, grid_locator2=gl2, tick_formatter1=tf1, tick_formatter2=None, ) ax = FA.FloatingSubplot(fig, *args, grid_helper=ghelper) fig.add_subplot(ax) # Adjust axes fontsize = 9 ax.axis["top"].set_axis_direction("bottom") # "Angle axis" ax.axis["top"].toggle(ticklabels=True, label=True) ax.axis["top"].major_ticklabels.set_size('xx-small') 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["top"].label.set_fontsize(fontsize) ax.axis["left"].set_axis_direction("right") # "X axis" ticks_font = 2 ax.axis["left"].major_ticklabels.set_size('xx-small') ax.axis["left"].major_ticks.set_ticksize(ticks_font) # "X axis" ax.axis["left"].label.set_text("Normalized standard deviation") ax.axis["left"].label.set_fontsize(fontsize) ax.axis["right"].major_ticklabels.set_size('xx-small') ax.axis["right"].set_axis_direction("top") # "Y axis" ax.axis["right"].toggle(ticklabels=True) ax.axis["right"].major_ticks.set_ticksize(ticks_font) # "Y axis" ax.axis["right"].major_ticklabels.set_axis_direction("left") ax.axis["right"].label.set_fontsize(fontsize) ax.axis["bottom"].set_visible(False) # turn off overlaying tickmark ax.grid(linewidth=0.5, color='gray') aux_ax = ax.get_aux_axes(tr) # Polar coordinates aux_ax.th1 = th1 aux_ax.th2 = th2 aux_ax.rd1 = rd1 aux_ax.rd2 = rd2 ax.th1 = th1 ax.th2 = th2 ax.rd1 = rd1 ax.rd2 = rd2 return ax, aux_ax
def __init__(self, refstd, fig=None, rect=None, label='_', srange=(0, 1.5), extend=False): """ Set up Taylor diagram axes, i.e. single quadrant polar plot, using `mpl_toolkits.axisartist.floating_axes`. Parameters: * refstd: reference standard deviation to be compared to * fig: input Figure or None * rect: subplot definition * label: reference label * srange: stddev axis extension, in units of *refstd* * extend: extend diagram to negative correlations """ self.refstd = refstd # Reference standard deviation tr = PolarAxes.PolarTransform() # Correlation labels rlocs = numpy.array([0, 0.2, 0.4, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99, 1]) if extend: # Diagram extended to negative correlations self.tmax = numpy.pi rlocs = numpy.concatenate((-rlocs[:0:-1], rlocs)) else: # Diagram limited to positive correlations self.tmax = numpy.pi / 2 tlocs = numpy.arccos(rlocs) # Conversion to polar angles gl1 = grid_finder.FixedLocator(tlocs) # Positions tf1 = grid_finder.DictFormatter(dict(zip(tlocs, map(str, rlocs)))) # set s locator gl2 = grid_finder.MaxNLocator(7) # Max 7 ticks # Standard deviation axis extent (in units of reference stddev) self.smin = srange[0] * self.refstd self.smax = srange[1] * self.refstd ghelper = floating_axes.GridHelperCurveLinear( tr, extremes=(0, self.tmax, self.smin, self.smax), grid_locator1=gl1, tick_formatter1=tf1, grid_locator2=gl2) self.fig = plt.figure() if fig is None else fig if rect is None: rect = 111 ax = floating_axes.FloatingSubplot(self.fig, rect, grid_helper=ghelper) self.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("bottom") # "X axis" ax.axis["left"].label.set_text("Standard deviation") ax.axis["right"].set_axis_direction("top") # "Y-axis" ax.axis["right"].toggle(ticklabels=True) ax.axis["right"].major_ticklabels.set_axis_direction( "bottom" if extend else "left") if self.smin: ax.axis["bottom"].toggle(ticklabels=False, label=False) else: ax.axis["bottom"].set_visible(False) # Unused self._ax = ax # Graphical axes self.ax = ax.get_aux_axes(tr) # Polar coordinates # Add reference point and stddev contour l, = self.ax.plot([0], self.refstd, 'k*', ls='', ms=10, label=label) t = numpy.linspace(0, self.tmax) r = numpy.zeros_like(t) + self.refstd self.ax.plot(t, r, 'k--', label='_', zorder=1) # Collect sample points for latter use (e.g. legend) self.sample_points = [l] # set color and marker styles self.ax.set_prop_cycle(get_point_style_cycler())