|
| 1 | +import numpy as np |
| 2 | +#from matplotlib.path import Path |
| 3 | + |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import matplotlib.cbook as cbook |
| 6 | + |
| 7 | +from mpl_toolkits.axes_grid.grid_helper_curvelinear import GridHelperCurveLinear |
| 8 | +from mpl_toolkits.axes_grid.axislines import Subplot |
| 9 | + |
| 10 | +from mpl_toolkits.axes_grid.parasite_axes import SubplotHost, \ |
| 11 | + ParasiteAxesAuxTrans |
| 12 | + |
| 13 | + |
| 14 | +def curvelinear_test1(fig): |
| 15 | + """ |
| 16 | + grid for custom transform. |
| 17 | + """ |
| 18 | + |
| 19 | + def tr(x, y): |
| 20 | + x, y = np.asarray(x), np.asarray(y) |
| 21 | + return x, y-x |
| 22 | + |
| 23 | + def inv_tr(x,y): |
| 24 | + x, y = np.asarray(x), np.asarray(y) |
| 25 | + return x, y+x |
| 26 | + |
| 27 | + |
| 28 | + grid_helper = GridHelperCurveLinear((tr, inv_tr)) |
| 29 | + |
| 30 | + ax1 = Subplot(fig, 1, 2, 1, grid_helper=grid_helper) |
| 31 | + # ax1 will have a ticks and gridlines defined by the given |
| 32 | + # transform (+ transData of the Axes). Note that the transform of |
| 33 | + # the Axes itself (i.e., transData) is not affected by the given |
| 34 | + # transform. |
| 35 | + |
| 36 | + fig.add_subplot(ax1) |
| 37 | + |
| 38 | + xx, yy = tr([3, 6], [5.0, 10.]) |
| 39 | + ax1.plot(xx, yy) |
| 40 | + |
| 41 | + ax1.set_aspect(1.) |
| 42 | + ax1.set_xlim(0, 10.) |
| 43 | + ax1.set_ylim(0, 10.) |
| 44 | + |
| 45 | + ax1.grid(True) |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +import mpl_toolkits.axes_grid.angle_helper as angle_helper |
| 50 | +from matplotlib.projections import PolarAxes |
| 51 | +from matplotlib.transforms import Affine2D |
| 52 | + |
| 53 | +def curvelinear_test2(fig): |
| 54 | + """ |
| 55 | + polar projection, but in a rectangular box. |
| 56 | + """ |
| 57 | + |
| 58 | + # PolarAxes.PolarTransform takes radian. However, we want our coordinate |
| 59 | + # system in degree |
| 60 | + tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() |
| 61 | + |
| 62 | + # polar projection, which involves cycle, and also has limits in |
| 63 | + # its coordinates, needs a special method to find the extremes |
| 64 | + # (min, max of the coordinate within the view). |
| 65 | + |
| 66 | + # 20, 20 : number of sampling points along x, y direction |
| 67 | + extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, |
| 68 | + lon_cycle = 360, |
| 69 | + lat_cycle = None, |
| 70 | + lon_minmax = None, |
| 71 | + lat_minmax = (0, np.inf), |
| 72 | + ) |
| 73 | + |
| 74 | + grid_locator1 = angle_helper.LocatorDMS(12) |
| 75 | + # Find a grid values appropriate for the coordinate (degree, |
| 76 | + # minute, second). |
| 77 | + |
| 78 | + tick_formatter1 = angle_helper.FormatterDMS() |
| 79 | + # And also uses an appropriate formatter. Note that,the |
| 80 | + # acceptable Locator and Formatter class is a bit different than |
| 81 | + # that of mpl's, and you cannot directly use mpl's Locator and |
| 82 | + # Formatter here (but may be possible in the future). |
| 83 | + |
| 84 | + grid_helper = GridHelperCurveLinear(tr, |
| 85 | + extreme_finder=extreme_finder, |
| 86 | + grid_locator1=grid_locator1, |
| 87 | + tick_formatter1=tick_formatter1 |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | + ax1 = SubplotHost(fig, 1, 2, 2, grid_helper=grid_helper) |
| 92 | + |
| 93 | + # make ticklabels of right and top axis visible. |
| 94 | + ax1.axis["right"].major_ticklabels.set_visible(True) |
| 95 | + ax1.axis["top"].major_ticklabels.set_visible(True) |
| 96 | + |
| 97 | + # let right axis shows ticklabels for 1st coordinate (angle) |
| 98 | + ax1.axis["right"].get_helper().nth_coord_ticks=0 |
| 99 | + # let bottom axis shows ticklabels for 2nd coordinate (radius) |
| 100 | + ax1.axis["bottom"].get_helper().nth_coord_ticks=1 |
| 101 | + |
| 102 | + fig.add_subplot(ax1) |
| 103 | + |
| 104 | + |
| 105 | + # A parasite axes with given transform |
| 106 | + ax2 = ParasiteAxesAuxTrans(ax1, tr, "equal") |
| 107 | + # note that ax2.transData == tr + ax1.transData |
| 108 | + # Anthing you draw in ax2 will match the ticks and grids of ax1. |
| 109 | + ax1.parasites.append(ax2) |
| 110 | + intp = cbook.simple_linear_interpolation |
| 111 | + ax2.plot(intp(np.array([0, 30]), 50), |
| 112 | + intp(np.array([10., 10.]), 50)) |
| 113 | + |
| 114 | + ax1.set_aspect(1.) |
| 115 | + ax1.set_xlim(-5, 12) |
| 116 | + ax1.set_ylim(-5, 10) |
| 117 | + |
| 118 | + ax1.grid(True) |
| 119 | + |
| 120 | +if 1: |
| 121 | + fig = plt.figure(1, figsize=(7, 4)) |
| 122 | + fig.clf() |
| 123 | + |
| 124 | + curvelinear_test1(fig) |
| 125 | + curvelinear_test2(fig) |
| 126 | + |
| 127 | + plt.draw() |
| 128 | + plt.show() |
| 129 | + |
| 130 | + |
| 131 | + |
0 commit comments