|
| 1 | + |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import mpl_toolkits.axes_grid.angle_helper as angle_helper |
| 5 | +import mpl_toolkits.axes_grid.grid_finder as grid_finder |
| 6 | +from matplotlib.projections import PolarAxes |
| 7 | +from matplotlib.transforms import Affine2D |
| 8 | + |
| 9 | +import mpl_toolkits.axes_grid.axislines as axislines |
| 10 | + |
| 11 | +from mpl_toolkits.axes_grid.grid_helper_curvelinear import GridHelperCurveLinear |
| 12 | + |
| 13 | + |
| 14 | +def setup_axes(fig, rect): |
| 15 | + """ |
| 16 | + polar projection, but in a rectangular box. |
| 17 | + """ |
| 18 | + |
| 19 | + # see demo_curvelinear_grid.py for details |
| 20 | + tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() |
| 21 | + |
| 22 | + extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, |
| 23 | + lon_cycle = 360, |
| 24 | + lat_cycle = None, |
| 25 | + lon_minmax = None, |
| 26 | + lat_minmax = (0, np.inf), |
| 27 | + ) |
| 28 | + |
| 29 | + grid_locator1 = angle_helper.LocatorDMS(12) |
| 30 | + grid_locator2 = grid_finder.MaxNLocator(5) |
| 31 | + |
| 32 | + tick_formatter1 = angle_helper.FormatterDMS() |
| 33 | + |
| 34 | + grid_helper = GridHelperCurveLinear(tr, |
| 35 | + extreme_finder=extreme_finder, |
| 36 | + grid_locator1=grid_locator1, |
| 37 | + grid_locator2=grid_locator2, |
| 38 | + tick_formatter1=tick_formatter1 |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | + ax1 = axislines.Subplot(fig, rect, grid_helper=grid_helper) |
| 43 | + ax1.axis[:].toggle(ticklabels=False) |
| 44 | + |
| 45 | + fig.add_subplot(ax1) |
| 46 | + |
| 47 | + ax1.set_aspect(1.) |
| 48 | + ax1.set_xlim(-5, 12) |
| 49 | + ax1.set_ylim(-5, 10) |
| 50 | + |
| 51 | + #ax1.grid(True) |
| 52 | + |
| 53 | + return ax1 |
| 54 | + |
| 55 | + |
| 56 | +def add_floating_axis1(ax1): |
| 57 | + ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 30) |
| 58 | + axis.label.set_text(r"$\theta = 30^{\circ}$") |
| 59 | + axis.label.set_visible(True) |
| 60 | + |
| 61 | + return axis |
| 62 | + |
| 63 | + |
| 64 | +def add_floating_axis2(ax1): |
| 65 | + ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) |
| 66 | + axis.label.set_text(r"$r = 6$") |
| 67 | + axis.label.set_visible(True) |
| 68 | + |
| 69 | + return axis |
| 70 | + |
| 71 | + |
| 72 | +import matplotlib.pyplot as plt |
| 73 | +fig = plt.figure(1, figsize=(8, 4.)) |
| 74 | +fig.clf() |
| 75 | +fig.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99, |
| 76 | + wspace=0.01, hspace=0.01) |
| 77 | + |
| 78 | +for i, d in enumerate(["bottom", "left", "top", "right"]): |
| 79 | + ax1 = setup_axes(fig, rect=241++i) |
| 80 | + axis = add_floating_axis1(ax1) |
| 81 | + axis.set_axis_direction(d) |
| 82 | + ax1.annotate(d, (0, 1), (5, -5), |
| 83 | + xycoords="axes fraction", textcoords="offset points", |
| 84 | + va="top", ha="left") |
| 85 | + |
| 86 | +for i, d in enumerate(["bottom", "left", "top", "right"]): |
| 87 | + ax1 = setup_axes(fig, rect=245++i) |
| 88 | + axis = add_floating_axis2(ax1) |
| 89 | + axis.set_axis_direction(d) |
| 90 | + ax1.annotate(d, (0, 1), (5, -5), |
| 91 | + xycoords="axes fraction", textcoords="offset points", |
| 92 | + va="top", ha="left") |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +plt.show() |
| 97 | + |
| 98 | + |
0 commit comments