def _buildTransform(current_axes): global _stlp_data_transform, _stlp_xlabel_transform, _stlp_ylabel_transform current_figure = current_axes.figure current_axes.axes.get_xaxis().set_visible(False) current_axes.axes.get_yaxis().set_visible(False) # pylab.box(False) data_figure_trans = current_axes.transData + current_figure.transFigure.inverted( ) pylab.xlim((_T_min, _T_max)) pylab.ylim((_p_min, _p_max)) identity_matrix = np.zeros((3, 3)) for idx in range(3): identity_matrix[idx, idx] = 1 # Create the affine matrix for the skew transform. This only works in data coordinates. We'll fix that later ... skew_matrix = np.copy(identity_matrix) skew_matrix[0, 1] = np.tan(45 * np.pi / 180) skew_transform = transforms.Affine2D(skew_matrix) # Create the logarithmic transform in the y. log_p_transform = transforms.blended_transform_factory( transforms.Affine2D(), LogScale(current_axes.yaxis, basey=10).get_transform()) # The log transform shrinks everything to log(p) space, so define a scale factor to blow it back up to a reasonable size. p_bnd_trans = log_p_transform.transform( np.array([[0, _p_min], [0, _p_max]]))[:, 1] scale_factor = (_p_max - _p_min) / (p_bnd_trans[1] - p_bnd_trans[0]) # Define the affine transform for the flip and another for the scale back to reasonable coordinates after the log transform. flip_transform = transforms.Affine2D.identity().scale(1, -1) preskew_scale_transform = transforms.Affine2D().translate( 0, p_bnd_trans[1]).scale(1, scale_factor).translate(0, _p_min) postskew_move_transform = transforms.Affine2D().translate(0, _p_min) # Define a transform that basically does everything but the skew so we can figure out where the 1000 mb level is and skew around that line. prelim_data_transform = log_p_transform + flip_transform + preskew_scale_transform + data_figure_trans marker = prelim_data_transform.transform(np.array([[_T_min, 1000]]))[0, 1] # Add a translation to that marker point into the data-figure transform matrix. data_figure_trans += transforms.Affine2D().translate(0, -marker) # Define our skew transform in figure coordinates. figure_skew_transform = data_figure_trans + skew_transform + data_figure_trans.inverted( ) # Create our skew-T log-p transform matrix. It does the log-p transform first, then the flip, then the scale, then the skew. _stlp_data_transform = log_p_transform + flip_transform + preskew_scale_transform + figure_skew_transform + current_axes.transData # Create a blended transform where the y axis is the log-p, but the x axis is the axes transform (for adding pressure labels and wind barbs). _stlp_xlabel_transform = transforms.blended_transform_factory( _stlp_data_transform, current_axes.transAxes) _stlp_ylabel_transform = transforms.blended_transform_factory( current_axes.transAxes, _stlp_data_transform) return
def test_clipping_of_log(): # issue 804 M, L, C = Path.MOVETO, Path.LINETO, Path.CLOSEPOLY points = [(0.2, -99), (0.4, -99), (0.4, 20), (0.2, 20), (0.2, -99)] codes = [M, L, L, L, C] path = Path(points, codes) # something like this happens in plotting logarithmic histograms trans = mtransforms.BlendedGenericTransform( mtransforms.Affine2D(), LogScale.Log10Transform('clip')) tpath = trans.transform_path_non_affine(path) result = tpath.iter_segments(trans.get_affine(), clip=(0, 0, 100, 100), simplify=False) tpoints, tcodes = zip(*result) assert_allclose(tcodes, [M, L, L, L, C])
def test_clipping_of_log(): # issue 804 M, L, C = Path.MOVETO, Path.LINETO, Path.CLOSEPOLY points = [(0.2, -99), (0.4, -99), (0.4, 20), (0.2, 20), (0.2, -99)] codes = [M, L, L, L, C] path = Path(points, codes) # something like this happens in plotting logarithmic histograms trans = BlendedGenericTransform(Affine2D(), LogScale.Log10Transform('clip')) tpath = trans.transform_path_non_affine(path) result = tpath.iter_segments(trans.get_affine(), clip=(0, 0, 100, 100), simplify=False) tpoints, tcodes = list(zip(*result)) # Because y coordinate -99 is outside the clip zone, the first # line segment is effectively removed. That means that the closepoly # operation must be replaced by a move to the first point. assert np.allclose(tcodes, [M, M, L, L, L, C])