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 test_polar_box(): # Remove this line when this test image is regenerated. plt.rcParams['text.kerning_factor'] = 6 fig = plt.figure(figsize=(5, 5)) # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). 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) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 # let bottom axis shows ticklabels for 2nd coordinate (radius) ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 fig.add_subplot(ax1) ax1.axis["lat"] = axis = grid_helper.new_floating_axis(0, 45, axes=ax1) axis.label.set_text("Test") axis.label.set_visible(True) axis.get_helper().set_extremes(2, 12) ax1.axis["lon"] = axis = grid_helper.new_floating_axis(1, 6, axes=ax1) axis.label.set_text("Test 2") axis.get_helper().set_extremes(-180, 90) # A parasite axes with given transform ax2 = ParasiteAxes(ax1, tr, viewlim_mode="equal") assert ax2.transData == tr + ax1.transData # Anything you draw in ax2 will match the ticks and grids of ax1. ax1.parasites.append(ax2) ax2.plot(np.linspace(0, 30, 50), np.linspace(10, 10, 50)) ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
def curvelinear_test2(fig, rect=111): """ Polar projection, but in a rectangular box. """ # see demo_curvelinear_grid.py for details tr = Affine2D().translate(0, 90) + Affine2D().scale(np.pi / 180., 1.) + \ PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle( 10, 60, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(-90, np.inf), ) # Changes theta gridline count grid_locator1 = angle_helper.LocatorHMS(12) grid_locator2 = angle_helper.LocatorDMS(6) tick_formatter1 = angle_helper.FormatterHMS() tick_formatter2 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2) ax1 = SubplotHost(fig, rect, grid_helper=grid_helper) # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) ax1.axis["bottom"].major_ticklabels.set_visible(True) # let right and bottom axis show ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 ax1.axis["bottom"].get_helper().nth_coord_ticks = 0 # fig.add_subplot(ax1) grid_helper = ax1.get_grid_helper() # You may or may not need these - they set the view window explicitly # rather than using the default as determined by matplotlib with extreme # finder. ax1.set_aspect(1.) ax1.set_xlim(-4, 25) # moves the origin left-right in ax1 ax1.set_ylim(-2.5, 30) # moves the origin up-down ax1.set_ylabel('$DEC\,(^{\circ})$') ax1.set_xlabel('$RA\,(h)$') ax1.grid(True) # ax1.grid(linestyle='--', which='x') # either keyword applies to both # ax1.grid(linestyle=':', which='y') # sets of gridlines return ax1, tr
def curvelinear_test2(fig): """ polar projection, but in a rectangular box. """ global ax1 import numpy as np import mpl_toolkits.axisartist.angle_helper as angle_helper from matplotlib.projections import PolarAxes from matplotlib.transforms import Affine2D from mpl_toolkits.axisartist import SubplotHost from mpl_toolkits.axisartist import GridHelperCurveLinear # 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) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) # Now creates floating axis #grid_helper = ax1.get_grid_helper() # floating axis whose first coordinate (theta) is fixed at 60 ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60) axis.label.set_text(r"$\theta = 60^{\circ}$") axis.label.set_visible(True) # floating axis whose second coordinate (r) is fixed at 6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) axis.label.set_text(r"$r = 6$") ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
def create_axes(self, rect=111): """ Create a special AxisArtist to overlay grid coordinates. Much of this taken from the examples here: http://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html """ # from curved coordinate to rectlinear coordinate. def tr(x, y): x, y = np.asarray(x), np.asarray(y) return self(x, y) # from rectlinear coordinate to curved coordinate. def inv_tr(x, y): x, y = np.asarray(x), np.asarray(y) return self(x, y, inverse=True) # Cycle the coordinates extreme_finder = angle_helper.ExtremeFinderCycle(20, 20) # Find a grid values appropriate for the coordinate. # The argument is a approximate number of grid lines. grid_locator1 = angle_helper.LocatorD(8, include_last=False) grid_locator2 = angle_helper.LocatorD(6, include_last=False) # Format the values of the grid tick_formatter1 = FormatterFgcm() tick_formatter2 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear( (tr, inv_tr), extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2, ) fig = plt.gcf() ax = axisartist.Subplot(fig, rect, grid_helper=grid_helper) fig.add_subplot(ax) ax.axis['left'].major_ticklabels.set_visible(True) ax.axis['right'].major_ticklabels.set_visible(True) ax.axis['bottom'].major_ticklabels.set_visible(True) ax.axis['top'].major_ticklabels.set_visible(True) ax.set_xlabel("Right Ascension") ax.set_ylabel("Declination") return fig, ax
def curvelinear_test2(fig): """ Polar projection, but in a rectangular box. """ # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi/180, 1) + PolarAxes.PolarTransform() # Polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). extreme_finder = angle_helper.ExtremeFinderCycle( nx=20, ny=20, # Number of sampling points in each direction. lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf), ) # Find grid values appropriate for the coordinate (degree, minute, second). grid_locator1 = angle_helper.LocatorDMS(12) # Use an appropriate formatter. Note that the acceptable Locator and # Formatter classes are a bit different than that of Matplotlib, which # cannot directly be used here (this may be possible in the future). tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear( tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper) # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 # let bottom axis shows ticklabels for 2nd coordinate (radius) ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 fig.add_subplot(ax1) ax1.set_aspect(1) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True, zorder=0) # A parasite axes with given transform ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") # note that ax2.transData == tr + ax1.transData # Anything you draw in ax2 will match the ticks and grids of ax1. ax1.parasites.append(ax2) ax2.plot(np.linspace(0, 30, 51), np.linspace(10, 10, 51), linewidth=2)
def curvelinear_test(fig): """Polar projection, but in a rectangular box. """ # 创建一个极坐标变换。PolarAxes.PolarTransform使用弧度,但本例 # 要设置的坐标系中角度的单位为度 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) # 使用适当的Formatter。请注意,可接受的Locator和Formatter类 # 与Matplotlib中的相应类稍有不同,后者目前还不能直接在这里使用 tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) # 创建浮动坐标轴 # 浮动坐标轴的第一个坐标(theta)指定为60度 ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60) axis.label.set_text(r"$\theta = 60^{\circ}$") axis.label.set_visible(True) # 浮动坐标轴的第二个坐标(r)指定为6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) axis.label.set_text(r"$r = 6$") ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
def curvelinear_test1(fig): """ grid for custom transform. """ def tr(x, y): sgn = np.sign(x) x, y = np.abs(np.asarray(x)), np.asarray(y) return sgn * x**.5, y def inv_tr(x, y): sgn = np.sign(x) x, y = np.asarray(x), np.asarray(y) return sgn * x**2, y extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=None, lat_cycle=None, # (0, np.inf), lon_minmax=None, lat_minmax=None, ) grid_helper = GridHelperCurveLinear((tr, inv_tr), extreme_finder=extreme_finder) ax1 = Subplot(fig, 111, grid_helper=grid_helper) # ax1 will have a ticks and gridlines defined by the given # transform (+ transData of the Axes). Note that the transform of # the Axes itself (i.e., transData) is not affected by the given # transform. fig.add_subplot(ax1) ax1.imshow(np.arange(25).reshape(5, 5), vmax=50, cmap=plt.cm.gray_r, interpolation="nearest", origin="lower") # tick density grid_helper.grid_finder.grid_locator1._nbins = 6 grid_helper.grid_finder.grid_locator2._nbins = 6
def curvelinear_test2(fig): """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) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = fig.add_subplot(axes_class=HostAxes, grid_helper=grid_helper) # Now creates floating axis # floating axis whose first coordinate (theta) is fixed at 60 ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60) axis.label.set_text(r"$\theta = 60^{\circ}$") axis.label.set_visible(True) # floating axis whose second coordinate (r) is fixed at 6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) axis.label.set_text(r"$r = 6$") ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
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 create_cg(fig=None, subplot=111, rot=-450, scale=-1, angular_spacing=10, radial_spacing=10, latmin=0, lon_cycle=360): """ Helper function to create curvelinear grid The function makes use of the Matplotlib AXISARTIST namespace `mpl_toolkits.axisartist \ <https://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html>`_. Here are some limitations to normal Matplotlib Axes. While using the Matplotlib `AxesGrid Toolkit \ <https://matplotlib.org/mpl_toolkits/axes_grid/index.html>`_ most of the limitations can be overcome. See `Matplotlib AxesGrid Toolkit User’s Guide \ <https://matplotlib.org/mpl_toolkits/axes_grid/users/index.html>`_. Parameters ---------- fig : matplotlib Figure object If given, the PPI/RHI will be plotted into this figure object. Axes are created as needed. If None a new figure object will be created or current figure will be used, depending on "subplot". subplot : :class:`matplotlib:matplotlib.gridspec.GridSpec`, \ matplotlib grid definition nrows/ncols/plotnumber, see examples section defaults to '111', only one subplot rot : float Rotation of the source data in degrees, defaults to -450 for PPI, use 0 for RHI scale : float Scale of source data, defaults to -1. for PPI, use 1 for RHI angular_spacing : float Spacing of the angular grid, defaults to 10. radial_spacing : float Spacing of the radial grid, defaults to 10. latmin : float Startvalue for radial grid, defaults to 0. lon_cycle : float Angular cycle, defaults to 360. Returns ------- cgax : matplotlib toolkit axisartist Axes object curvelinear Axes (r-theta-grid) caax : matplotlib Axes object (twin to cgax) Cartesian Axes (x-y-grid) for plotting cartesian data paax : matplotlib Axes object (parasite to cgax) The parasite axes object for plotting polar data """ # create transformation # rotate tr_rotate = Affine2D().translate(rot, 0) # scale tr_scale = Affine2D().scale(scale * np.pi / 180, 1) # polar tr_polar = PolarAxes.PolarTransform() tr = tr_rotate + tr_scale + tr_polar # build up curvelinear grid extreme_finder = ah.ExtremeFinderCycle( 360, 360, lon_cycle=lon_cycle, lat_cycle=None, lon_minmax=None, lat_minmax=(latmin, np.inf), ) # locator and formatter for angular annotation grid_locator1 = ah.LocatorDMS(lon_cycle // angular_spacing) tick_formatter1 = ah.FormatterDMS() # grid_helper for curvelinear grid grid_helper = GridHelperCurveLinear( tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=None, tick_formatter1=tick_formatter1, tick_formatter2=None, ) # try to set nice locations for radial gridlines grid_locator2 = grid_helper.grid_finder.grid_locator2 grid_locator2._nbins = (radial_spacing * 2 + 1) // np.sqrt(2) # if there is no figure object given if fig is None: # create new figure if there is only one subplot if subplot == 111: fig = pl.figure() # otherwise get current figure or create new figure else: fig = pl.gcf() # generate Axis cgax = SubplotHost(fig, subplot, grid_helper=grid_helper) fig.add_axes(cgax) # get twin axis for cartesian grid caax = cgax.twin() # move axis annotation from right to left and top to bottom for # cartesian axis caax.toggle_axisline() # make right and top axis visible and show ticklabels (curvelinear axis) cgax.axis["top", "right"].set_visible(True) cgax.axis["top", "right"].major_ticklabels.set_visible(True) # make ticklabels of left and bottom axis invisible (curvelinear axis) cgax.axis["left", "bottom"].major_ticklabels.set_visible(False) # and also set tickmarklength to zero for better presentation # (curvelinear axis) cgax.axis["top", "right", "left", "bottom"].major_ticks.set_ticksize(0) # show theta (angles) on top and right axis cgax.axis["top"].get_helper().nth_coord_ticks = 0 cgax.axis["right"].get_helper().nth_coord_ticks = 0 # generate and add parasite axes with given transform paax = ParasiteAxesAuxTrans(cgax, tr, "equal") # note that paax.transData == tr + cgax.transData # Anything you draw in paax will match the ticks and grids of cgax. cgax.parasites.append(paax) return cgax, caax, paax
def create_axes(self, rect=111): """ Create a special AxisArtist to overlay grid coordinates. Much of this taken from the examples here: http://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html """ # from curved coordinate to rectlinear coordinate. def tr(x, y): return self(x, y) # from rectlinear coordinate to curved coordinate. def inv_tr(x, y): return self(x, y, inverse=True) # Cycle the coordinates extreme_finder = angle_helper.ExtremeFinderCycle(20, 20) # Find a grid values appropriate for the coordinate. # The argument is a approximate number of grid lines. grid_locator1 = angle_helper.LocatorD(9, include_last=False) # grid_locator1 = angle_helper.LocatorD(8, include_last=False) grid_locator2 = angle_helper.LocatorD(6, include_last=False) # Format the values of the grid tick_formatter1 = self.create_tick_formatter() tick_formatter2 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear( (tr, inv_tr), extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2, ) fig = plt.gcf() rect = self.ax.get_position() ax = axisartist.Axes(fig, rect, grid_helper=grid_helper, frameon=False) fig.add_axes(ax) # Coordinate formatter def format_coord(x, y): return 'lon=%1.4f, lat=%1.4f' % inv_tr(x, y) ax.format_coord = format_coord ax.axis['left'].major_ticklabels.set_visible(True) ax.axis['right'].major_ticklabels.set_visible(False) ax.axis['bottom'].major_ticklabels.set_visible(True) ax.axis['top'].major_ticklabels.set_visible(True) ax.axis['bottom'].label.set(text="Right Ascension", size=18) ax.axis['left'].label.set(text="Declination", size=18) self.aa = ax # Set the current axis back to the SkyAxes fig.sca(self.ax) return fig, ax
def curvelinear_test2(fig): """ polar projection, but in a rectangular box. """ # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction 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) # Find a grid values appropriate for the coordinate (degree, # minute, second). tick_formatter1 = angle_helper.FormatterDMS() # And also uses an appropriate formatter. Note that,the # acceptable Locator and Formatter class is a bit different than # that of mpl's, and you cannot directly use mpl's Locator and # Formatter here (but may be possible in the future). grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper) # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 # let bottom axis shows ticklabels for 2nd coordinate (radius) ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 fig.add_subplot(ax1) # A parasite axes with given transform ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") # note that ax2.transData == tr + ax1.transData # Anthing you draw in ax2 will match the ticks and grids of ax1. ax1.parasites.append(ax2) intp = cbook.simple_linear_interpolation ax2.plot(intp(np.array([0, 30]), 50), intp(np.array([10., 10.]), 50), linewidth=2.0) ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True, zorder=0)
def curvelinear_test2(fig, gs=None, xcenter=0.0, ycenter=17.3, xwidth=1.5, ywidth=1.5, rot_angle=0.0, xlabel=xlabel, ylabel=ylabel, xgrid_density=8, ygrid_density=5): """ polar projection, but in a rectangular box. """ tr = Affine2D().translate(0,90) tr += Affine2D().scale(np.pi/180., 1.) tr += PolarAxes.PolarTransform() tr += Affine2D().rotate(rot_angle) # This rotates the grid extreme_finder = angle_helper.ExtremeFinderCycle(10, 60, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (-90, np.inf), ) grid_locator1 = angle_helper.LocatorHMS(xgrid_density) #changes theta gridline count tick_formatter1 = angle_helper.FormatterHMS() grid_locator2 = angle_helper.LocatorDMS(ygrid_density) #changes theta gridline count tick_formatter2 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2 ) # ax1 = SubplotHost(fig, rect, grid_helper=grid_helper) if gs is None: ax1 = SubplotHost(fig, 111, grid_helper=grid_helper) else: ax1 = SubplotHost(fig, gs, grid_helper=grid_helper) # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(False) ax1.axis["top"].major_ticklabels.set_visible(False) ax1.axis["bottom"].major_ticklabels.set_visible(True) #Turn off? # let right and bottom axis show ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks=0 ax1.axis["bottom"].get_helper().nth_coord_ticks=0 fig.add_subplot(ax1) grid_helper = ax1.get_grid_helper() # These move the grid ax1.set_xlim(xcenter-xwidth, xcenter+xwidth) # moves the origin left-right in ax1 ax1.set_ylim(ycenter-ywidth, ycenter+ywidth) # moves the origin up-down if xlabel is not None: ax1.set_xlabel(xlabel) if ylabel is not None: ax1.set_ylabel(ylabel) ax1.grid(True, linestyle='-') return ax1,tr
def curvelinear_plot(rayon_max=1000): """Fonction pour créer un repère cartésien avec en décoration les coordonnées polaire pour mieux repérer les angles Cette fonction est basée sur un exemple matplotlib : see demo_curvelinear_grid.py for details Args: rayon_max (float, optional): Rayon amximal du plot dans lequel afficher le robot. Defaults to 1000. Returns: matplotlib.fig, matplotlib.ax : figure et axe matplotlib dans lequel on fait l'affichage """ tr_rotate = Affine2D().translate(90, 0) tr_scale = Affine2D().scale(np.pi / 180., 1.) tr = tr_rotate + tr_scale + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(-np.inf, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(8) # tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear( tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, # tick_formatter1=tick_formatter1 ) fig = plt.figure() ax1 = fig.add_subplot(axes_class=HostAxes, grid_helper=grid_helper) # Now creates floating axis # ax1.scatter(5, 5) # floating axis whose first coordinate (theta) is fixed at 60 ax1.axis["ax"] = axis = ax1.new_floating_axis(0, 0) axis.set_axis_direction("top") axis.major_ticklabels.set_axis_direction("left") ax1.axis["ax1"] = axis = ax1.new_floating_axis(0, -90) axis.set_axis_direction("left") axis.major_ticklabels.set_axis_direction("top") # axis.label.set_text(r"$\theta = 60^{\circ}$") # axis.label.set_visible(True) # floating axis whose second coordinate (r) is fixed at 6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 150) axis.label.set_pad(10) # axis.label.set_text(r"$r = 1$") ax1.set_aspect(1.) ax1.set_xlim(-rayon_max, rayon_max) ax1.set_ylim(-rayon_max, rayon_max) ax1.grid(True) return fig, ax1
def make_polar_axis(figure): """ Generate a polar axis. Examples -------- >>> from pylab import * >>> f = figure() >>> ax1,ax2 = make_polar_axis(f) >>> f.add_subplot(ax1) """ # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction extreme_finder = angle_helper.ExtremeFinderCycle( 40, 40, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(12) # Find a grid values appropriate for the coordinate (degree, # minute, second). tick_formatter1 = angle_helper.FormatterDMS() # And also uses an appropriate formatter. Note that,the # acceptable Locator and Formatter class is a bit different than # that of mpl's, and you cannot directly use mpl's Locator and # Formatter here (but may be possible in the future). grid_helper = GridHelperCurveLinear( tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1, #tick_formatter2=matplotlib.ticker.FuncFormatter(lambda x: x * kpc_per_pix) ) ax1 = SubplotHost(figure, 1, 1, 1, grid_helper=grid_helper, axisbg='#333333') # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 # let bottom axis shows ticklabels for 2nd coordinate (radius) ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") ax1.parasites.append(ax2) return ax1, ax2
def make_mw_plot(fig=None, mw_img_name = "Milky_Way_2005.jpg", solar_rad=8.5, fignum=5): """ Generate a "Milky Way" plot with Robert Hurt's Milky Way illustration as the background. .. TODO: Figure out how to fix the axis labels. They don't work now! Parameters ---------- fig : matplotlib.figure instance If you want to start with a figure instance, can specify it mw_img_name: str The name of the image on disk solar_rad : float The assumed Galactocentric orbital radius of the sun fignum : int If Figure not specified, use this figure number """ # load image mw = np.array(PIL.Image.open(mw_img_name))[:,::-1] # set some constants npix = mw.shape[0] # must be symmetric # Galactic Center in middle of image gc_loc = [x/2 for x in mw.shape] # Sun is at 0.691 (maybe really 0.7?) length of image sun_loc = mw.shape[0]/2,int(mw.shape[1]*0.691) # determine scaling kpc_per_pix = solar_rad / (sun_loc[1]-gc_loc[1]) boxsize = npix*kpc_per_pix # most of the code below is taken from: # http://matplotlib.sourceforge.net/examples/axes_grid/demo_curvelinear_grid.html # and http://matplotlib.sourceforge.net/examples/axes_grid/demo_floating_axis.html if fig is None: fig = plt.figure(fignum) plt.clf() # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree # this defines the polar coordinate system @ Galactic center tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # grid helper stuff, I think (grid is off by default) # This may not apply to the image *at all*, but would if you # used the grid # 40, 40 : number of sampling points along x, y direction extreme_finder = angle_helper.ExtremeFinderCycle(40, 40, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(12) # Find a grid values appropriate for the coordinate (degree, # minute, second). tick_formatter1 = angle_helper.FormatterDMS() # And also uses an appropriate formatter. Note that,the # acceptable Locator and Formatter class is a bit different than # that of mpl's, and you cannot directly use mpl's Locator and # Formatter here (but may be possible in the future). grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1, #tick_formatter2=matplotlib.ticker.FuncFormatter(lambda x: x * kpc_per_pix) ) ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper, axisbg='#333333') fig.add_subplot(ax) # ax.transData is still a (rectlinear) pixel coordinate. Only the # grids are done in galactocentric coordinate. # show the image ax.imshow(mw,extent=[-boxsize/2,boxsize/2,-boxsize/2,boxsize/2]) ax_pixgrid = ax.twin() # simple twin will give you a twin axes, # but with normal grids. # to draw heliocentric grids, it is best to update the grid_helper # with new transform. # need to rotate by -90 deg to get into the standard convention tr_helio = Affine2D().scale(np.pi/180., 1.).translate(-np.pi/2.,0) + \ PolarAxes.PolarTransform() + \ Affine2D().translate(0,solar_rad) # Note that the transform is from the heliocentric coordinate to # the pixel coordinate of ax (i.e., ax.transData). ax.get_grid_helper().update_grid_finder(aux_trans=tr_helio) # Now we defina parasite axes with galactocentric & heliocentric # coordinates. # A parasite axes with given transform gc_polar = ParasiteAxesAuxTrans(ax, tr, "equal") ax.parasites.append(gc_polar) # note that ax2.transData == tr + galactocentric_axis.transData # Anthing you draw in ax2 will match the ticks and grids of galactocentric_axis. hc_polar = ParasiteAxesAuxTrans(ax, tr_helio, "equal") ax.parasites.append(hc_polar) return ax, ax_pixgrid, gc_polar, hc_polar
def test_axis_direction(): fig = plt.figure(figsize=(5, 5)) # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction 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) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) for axis in ax1.axis.values(): axis.set_visible(False) fig.add_subplot(ax1) ax1.axis["lat1"] = axis = grid_helper.new_floating_axis( 0, 130, axes=ax1, axis_direction="left") axis.label.set_text("Test") axis.label.set_visible(True) axis.get_helper()._extremes = 0.001, 10 ax1.axis["lat2"] = axis = grid_helper.new_floating_axis( 0, 50, axes=ax1, axis_direction="right") axis.label.set_text("Test") axis.label.set_visible(True) axis.get_helper()._extremes = 0.001, 10 ax1.axis["lon"] = axis = grid_helper.new_floating_axis( 1, 10, axes=ax1, axis_direction="bottom") axis.label.set_text("Test 2") axis.get_helper()._extremes = 50, 130 axis.major_ticklabels.set_axis_direction("top") axis.label.set_axis_direction("top") grid_helper.grid_finder.grid_locator1.den = 5 grid_helper.grid_finder.grid_locator2._nbins = 5 ax1.set_aspect(1.) ax1.set_xlim(-8, 8) ax1.set_ylim(-4, 12) ax1.grid(True)
def plotGsrResidue(theta, phi, residue, optTheta, optPhi, mvabTheta=None, mvabPhi=None, mvubTheta=None, mvubPhi=None): fig = figure() fig.clf() # some matplotlib setup stuff which I don't fully understand but it works tr = Affine2D().scale(pi / 180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, inf), ) grid_locator1 = angle_helper.LocatorDMS(12) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) ax1.axis["right"].get_helper().nth_coord_ticks = 0 ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 # draw the filled contoured map in polar coordinates ax1.contour(transpose(mat(theta)) * mat(cos(phi * pi / 180)), transpose(mat(theta)) * mat(sin(phi * pi / 180)), 1 / transpose(reshape(residue, (phi.size, -1))), 100, lw=0.1) cc = ax1.contourf( transpose(mat(theta)) * mat(cos(phi * pi / 180)), transpose(mat(theta)) * mat(sin(phi * pi / 180)), 1 / transpose(reshape(residue, (phi.size, -1))), 100) # remove gaps between the contour lines for c in cc.collections: c.set_antialiased(False) # show the MVAB direction if mvabTheta is not None and mvabPhi is not None: ax1.plot(mvabTheta * cos(mvabPhi * pi / 180), mvabTheta * sin(mvabPhi * pi / 180), 'sk', markersize=8) # show the MVUB direction if mvubTheta is not None and mvubPhi is not None: ax1.plot(mvubTheta * cos(mvubPhi * pi / 180), mvubTheta * sin(mvubPhi * pi / 180), 'dk', markersize=8) # show the optimal direction ax1.plot(optTheta * cos(optPhi * pi / 180), optTheta * sin(optPhi * pi / 180), '.k', markersize=15) # aspect and initial axes limits ax1.set_aspect(1.) ax1.set_xlim(-90, 90) ax1.set_ylim(-90, 90) # add grid ax1.grid(True) # add colobar cb = colorbar(cc, pad=0.07) cb.locator = MaxNLocator(14) cb.update_ticks() cb.set_label(r"$1/\tilde{\mathcal{R}}$") # save if toSave: savefig(resultsDir + '/eps/gsr_ResidualMap.eps', format='eps') savefig(resultsDir + '/png/gsr_ResidualMap.png', format='png')
def polar_stuff(fig, telescope): # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi / 180., 1.).translate( +np.pi / 2., 0) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction n = 1 extreme_finder = angle_helper.ExtremeFinderCycle( n, n, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(-90, 90), ) grid_locator1 = angle_helper.LocatorDMS(12) # Find a grid values appropriate for the coordinate (degree, # minute, second). tick_formatter1 = angle_helper.FormatterDMS() # And also uses an appropriate formatter. Note that,the # acceptable Locator and Formatter class is a bit different than # that of mpl's, and you cannot directly use mpl's Locator and # Formatter here (but may be possible in the future). grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) # make ticklabels of right and top axis visible. ax1.axis["right"].major_ticklabels.set_visible(True) ax1.axis["top"].major_ticklabels.set_visible(True) # let right axis shows ticklabels for 1st coordinate (angle) ax1.axis["right"].get_helper().nth_coord_ticks = 0 # let bottom axis shows ticklabels for 2nd coordinate (radius) ax1.axis["bottom"].get_helper().nth_coord_ticks = 1 fig.add_subplot(ax1) # A parasite axes with given transform ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") # note that ax2.transData == tr + ax1.transData # Anything you draw in ax2 will match the ticks and grids of ax1. ax1.parasites.append(ax2) # intp = cbook.simple_linear_interpolation #ax2.plot(intp(np.array([0, 30]), 50), # intp(np.array([10., 10.]), 50), # linewidth=2.0) x = np.rad2deg(telescope.az.value) * np.cos(telescope.alt.value) y = np.rad2deg(telescope.alt.value) circle = plt.Circle( (np.rad2deg(telescope.az.value - np.pi) * np.sin(telescope.alt.value), np.rad2deg(-telescope.alt.value * np.cos( (telescope.az.value - np.pi)))), radius=7.7 / 2, color="red", alpha=0.2, ) circle = plt.Circle( (x, y), radius=7.7 / 2, color="red", alpha=0.2, ) ax1.add_artist(circle) # point = ax1.scatter(x, y, c="b", s=20, zorder=10, transform=ax2.transData) ax2.annotate(1, (x, y), fontsize=15, xytext=(4, 4), textcoords='offset pixels') ax1.set_xlim(-180, 180) ax1.set_ylim(0, 90) ax1.set_aspect(1.) ax1.grid(True, zorder=0) ax1.set_xlabel("Azimuth in degrees", fontsize=20) ax1.set_ylabel("Zenith in degrees", fontsize=20) plt.show() return fig
def create_axes(self,rect=111): """ Create a special AxisArtist to overlay grid coordinates. Much of this taken from the examples here: http://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html """ # from curved coordinate to rectlinear coordinate. def tr(x, y): x, y = np.asarray(x), np.asarray(y) return self(x,y) # from rectlinear coordinate to curved coordinate. def inv_tr(x,y): x, y = np.asarray(x), np.asarray(y) return self(x,y,inverse=True) # Cycle the coordinates extreme_finder = angle_helper.ExtremeFinderCycle(20, 20) # Find a grid values appropriate for the coordinate. # The argument is a approximate number of grid lines. grid_locator1 = angle_helper.LocatorD(9,include_last=False) #grid_locator1 = angle_helper.LocatorD(8,include_last=False) grid_locator2 = angle_helper.LocatorD(6,include_last=False) # Format the values of the grid tick_formatter1 = self.create_tick_formatter() tick_formatter2 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear((tr, inv_tr), extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=tick_formatter2, ) fig = plt.gcf() if rect is None: # This doesn't quite work. Need to remove the existing axis... rect = plt.gca().get_position() plt.gca().axis('off') ax = axisartist.Axes(fig,rect,grid_helper=grid_helper) fig.add_axes(ax) else: ax = axisartist.Subplot(fig,rect,grid_helper=grid_helper) fig.add_subplot(ax) ## Coordinate formatter def format_coord(x, y): return 'lon=%1.4f, lat=%1.4f'%inv_tr(x,y) ax.format_coord = format_coord ax.axis['left'].major_ticklabels.set_visible(True) ax.axis['right'].major_ticklabels.set_visible(False) ax.axis['bottom'].major_ticklabels.set_visible(True) ax.axis['top'].major_ticklabels.set_visible(True) ax.set_xlabel("Right Ascension") ax.set_ylabel("Declination") #self.set_axes_limits() self.axisartist = ax return fig,ax
def __init__(self, fig=None, max_normed_std=2.5, std_ratios=[1], \ bias_vmin=None, bias_vmax=None, \ normalized=True, cmap=plt.cm.jet, s=80, title_expected=r'expected'): """ Set up Taylor diagram axes. """ self.title_polar = r'Correlation' self.title_xy = r'Normalized Standard Deviation' self.max_normed_std = max_normed_std self.s_min = 0 self.std_ratios = std_ratios self.bias_vmin = bias_vmin self.bias_vmax = bias_vmax self.normalized = normalized self.cmap = cmap self.s = s # marker size self.title_expected = title_expected # Define polar coordinate tr = 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) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) # figure self.fig = fig if self.fig is None: self.fig = plt.figure() # setup axes ax = SubplotHost(self.fig, 111, grid_helper=grid_helper) self.ax = self.fig.add_subplot(ax) # set x and y axis self._setup_axes() # attach the polar axes and plot the correlation lines self.polar_ax = self.ax.get_aux_axes(tr) self.polar_ax.plot([np.pi / 2.135, np.pi / 2.135], [self.s_min, self.max_normed_std * 2], color='grey') self.polar_ax.plot([np.pi / 2.484, np.pi / 2.484], [self.s_min, self.max_normed_std * 2], color='grey') self.polar_ax.plot([np.pi / 3, np.pi / 3], [self.s_min, self.max_normed_std * 2], color='grey') self.polar_ax.plot([np.pi / 3.95, np.pi / 3.95], [self.s_min, self.max_normed_std * 2], color='grey') self.polar_ax.plot([np.pi / 6.95, np.pi / 6.95], [self.s_min, self.max_normed_std * 2], color='grey') # Add norm stddev ratio contours self._plot_req_cont(self.std_ratios) self.points = []
def create_cg(st, fig=None, subplot=111): """ Helper function to create curvelinear grid The function makes use of the Matplotlib AXISARTIST namespace `mpl_toolkits.axisartist \ <https://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html>`_. Here are some limitations to normal Matplotlib Axes. While using the Matplotlib `AxesGrid Toolkit \ <https://matplotlib.org/mpl_toolkits/axes_grid/index.html>`_ most of the limitations can be overcome. See `Matplotlib AxesGrid Toolkit User’s Guide \ <https://matplotlib.org/mpl_toolkits/axes_grid/users/index.html>`_. Parameters ---------- st : string scan type, 'PPI' or 'RHI' fig : matplotlib Figure object If given, the PPI will be plotted into this figure object. Axes are created as needed. If None a new figure object will be created or current figure will be used, depending on "subplot". subplot : :class:`matplotlib:matplotlib.gridspec.GridSpec`, \ matplotlib grid definition nrows/ncols/plotnumber, see examples section defaults to '111', only one subplot Returns ------- cgax : matplotlib toolkit axisartist Axes object curvelinear Axes (r-theta-grid) caax : matplotlib Axes object (twin to cgax) Cartesian Axes (x-y-grid) for plotting cartesian data paax : matplotlib Axes object (parasite to cgax) The parasite axes object for plotting polar data """ if st == 'RHI': # create transformation tr = Affine2D().scale(np.pi / 180, 1) + PolarAxes.PolarTransform() # build up curvelinear grid extreme_finder = ah.ExtremeFinderCycle(20, 20, lon_cycle=100, lat_cycle=None, lon_minmax=(0, np.inf), lat_minmax=(0, np.inf), ) # locator and formatter for angular annotation grid_locator1 = ah.LocatorDMS(10.) tick_formatter1 = ah.FormatterDMS() # grid_helper for curvelinear grid grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=None, tick_formatter1=tick_formatter1, tick_formatter2=None, ) # try to set nice locations for range gridlines grid_helper.grid_finder.grid_locator2._nbins = 30.0 grid_helper.grid_finder.grid_locator2._steps = [0, 1, 1.5, 2, 2.5, 5, 10] if st == 'PPI': # Set theta start to north tr_rotate = Affine2D().translate(-90, 0) # set theta running clockwise tr_scale = Affine2D().scale(-np.pi / 180, 1) # create transformation tr = tr_rotate + tr_scale + PolarAxes.PolarTransform() # build up curvelinear grid extreme_finder = ah.ExtremeFinderCycle(20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=(360, 0), lat_minmax=(0, np.inf), ) # locator and formatter for angle annotation locs = [i for i in np.arange(0., 359., 10.)] grid_locator1 = FixedLocator(locs) tick_formatter1 = DictFormatter(dict([(i, r"${0:.0f}^\circ$".format(i)) for i in locs])) # grid_helper for curvelinear grid grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=None, tick_formatter1=tick_formatter1, tick_formatter2=None, ) # try to set nice locations for range gridlines grid_helper.grid_finder.grid_locator2._nbins = 15.0 grid_helper.grid_finder.grid_locator2._steps = [0, 1, 1.5, 2, 2.5, 5, 10] # if there is no figure object given if fig is None: # create new figure if there is only one subplot if subplot is 111: fig = pl.figure() # otherwise get current figure or create new figure else: fig = pl.gcf() # generate Axis cgax = SubplotHost(fig, subplot, grid_helper=grid_helper) fig.add_axes(cgax) # PPIs always plottetd with equal aspect if st == 'PPI': cgax.set_aspect('equal', adjustable='box') # get twin axis for cartesian grid caax = cgax.twin() # move axis annotation from right to left and top to bottom for # cartesian axis caax.toggle_axisline() # make right and top axis visible and show ticklabels (curvelinear axis) cgax.axis["top", "right"].set_visible(True) cgax.axis["top", "right"].major_ticklabels.set_visible(True) # make ticklabels of left and bottom axis invisible (curvelinear axis) cgax.axis["left", "bottom"].major_ticklabels.set_visible(False) # and also set tickmarklength to zero for better presentation # (curvelinear axis) cgax.axis["top", "right", "left", "bottom"].major_ticks.set_ticksize(0) # show theta (angles) on top and right axis cgax.axis["top"].get_helper().nth_coord_ticks = 0 cgax.axis["right"].get_helper().nth_coord_ticks = 0 # generate and add parasite axes with given transform paax = ParasiteAxesAuxTrans(cgax, tr, "equal") # note that paax.transData == tr + cgax.transData # Anything you draw in paax will match the ticks and grids of cgax. cgax.parasites.append(paax) return cgax, caax, paax