| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ | |||
| 3 | 3 | Rotating a 3D plot | |
| 4 | 4 | ================== | |
| 5 | 5 | ||
| 6 | - A very simple animation of a rotating 3D plot. | ||
| 6 | + A very simple animation of a rotating 3D plot about all 3 axes. | ||
| 7 | 7 | ||
| 8 | 8 | See wire3d_animation_demo for another simple example of animating a 3D plot. | |
| 9 | 9 | ||
@@ -17,12 +17,34 @@ | |||
| 17 | 17 | fig = plt.figure() | |
| 18 | 18 | ax = fig.add_subplot(projection='3d') | |
| 19 | 19 | ||
| 20 | - # load some test data for demonstration and plot a wireframe | ||
| 21 | - X, Y, Z = axes3d.get_test_data(0.1) | ||
| 22 | - ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5) | ||
| 20 | + # Grab some example data and plot a basic wireframe. | ||
| 21 | + X, Y, Z = axes3d.get_test_data(0.05) | ||
| 22 | + ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) | ||
| 23 | + | ||
| 24 | + # Set the axis labels | ||
| 25 | + ax.set_xlabel('x') | ||
| 26 | + ax.set_ylabel('y') | ||
| 27 | + ax.set_zlabel('z') | ||
| 28 | + | ||
| 29 | + # Rotate the axes and update | ||
| 30 | + for angle in range(0, 360*4 + 1): | ||
| 31 | + # Normalize the angle to the range [-180, 180] for display | ||
| 32 | + angle_norm = (angle + 180) % 360 - 180 | ||
| 33 | + | ||
| 34 | + # Cycle through a full rotation of elevation, then azimuth, roll, and all | ||
| 35 | + elev = azim = roll = 0 | ||
| 36 | + if angle <= 360: | ||
| 37 | + elev = angle_norm | ||
| 38 | + elif angle <= 360*2: | ||
| 39 | + azim = angle_norm | ||
| 40 | + elif angle <= 360*3: | ||
| 41 | + roll = angle_norm | ||
| 42 | + else: | ||
| 43 | + elev = azim = roll = angle_norm | ||
| 44 | + | ||
| 45 | + # Update the axis view and title | ||
| 46 | + ax.view_init(elev, azim, roll) | ||
| 47 | + plt.title('Elevation: %d°, Azimuth: %d°, Roll: %d°' % (elev, azim, roll)) | ||
| 23 | 48 | ||
| 24 | - # rotate the axes and update | ||
| 25 | - for angle in range(0, 360): | ||
| 26 | - ax.view_init(30, angle, 0) | ||
| 27 | 49 | plt.draw() | |
| 28 | 50 | plt.pause(.001) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | """ | |
| 2 | - ========================== | ||
| 3 | - Rotating 3D wireframe plot | ||
| 4 | - ========================== | ||
| 2 | + ============================= | ||
| 3 | + Animating a 3D wireframe plot | ||
| 4 | + ============================= | ||
| 5 | 5 | ||
| 6 | 6 | A very simple 'animation' of a 3D plot. See also rotate_axes3d_demo. | |
| 7 | 7 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,17 +109,14 @@ def zoom_effect02(ax1, ax2, **kwargs): | |||
| 109 | 109 | return c1, c2, bbox_patch1, bbox_patch2, p | |
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | - plt.figure(figsize=(5, 5)) | ||
| 113 | - ax1 = plt.subplot(221) | ||
| 114 | - ax2 = plt.subplot(212) | ||
| 115 | - ax2.set_xlim(0, 1) | ||
| 116 | - ax2.set_xlim(0, 5) | ||
| 117 | - zoom_effect01(ax1, ax2, 0.2, 0.8) | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - ax1 = plt.subplot(222) | ||
| 121 | - ax1.set_xlim(2, 3) | ||
| 122 | - ax2.set_xlim(0, 5) | ||
| 123 | - zoom_effect02(ax1, ax2) | ||
| 112 | + axs = plt.figure().subplot_mosaic([ | ||
| 113 | + ["zoom1", "zoom2"], | ||
| 114 | + ["main", "main"], | ||
| 115 | + ]) | ||
| 116 | + | ||
| 117 | + axs["main"].set(xlim=(0, 5)) | ||
| 118 | + zoom_effect01(axs["zoom1"], axs["main"], 0.2, 0.8) | ||
| 119 | + axs["zoom2"].set(xlim=(2, 3)) | ||
| 120 | + zoom_effect02(axs["zoom2"], axs["main"]) | ||
| 124 | 121 | ||
| 125 | 122 | plt.show() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -838,17 +838,10 @@ class FigureCanvasPS(FigureCanvasBase): | |||
| 838 | 838 | def get_default_filetype(self): | |
| 839 | 839 | return 'ps' | |
| 840 | 840 | ||
| 841 | - @_api.delete_parameter("3.5", "args") | ||
| 842 | - def print_ps(self, outfile, *args, **kwargs): | ||
| 843 | - return self._print_ps(outfile, 'ps', **kwargs) | ||
| 844 | - | ||
| 845 | - @_api.delete_parameter("3.5", "args") | ||
| 846 | - def print_eps(self, outfile, *args, **kwargs): | ||
| 847 | - return self._print_ps(outfile, 'eps', **kwargs) | ||
| 848 | - | ||
| 849 | 841 | @_api.delete_parameter("3.4", "dpi") | |
| 842 | + @_api.delete_parameter("3.5", "args") | ||
| 850 | 843 | def _print_ps( | |
| 851 | - self, outfile, format, *, | ||
| 844 | + self, fmt, outfile, *args, | ||
| 852 | 845 | dpi=None, metadata=None, papertype=None, orientation='portrait', | |
| 853 | 846 | **kwargs): | |
| 854 | 847 | ||
@@ -885,12 +878,12 @@ def _print_ps( | |||
| 885 | 878 | printer = (self._print_figure_tex | |
| 886 | 879 | if mpl.rcParams['text.usetex'] else | |
| 887 | 880 | self._print_figure) | |
| 888 | - printer(outfile, format, dpi=dpi, dsc_comments=dsc_comments, | ||
| 881 | + printer(fmt, outfile, dpi=dpi, dsc_comments=dsc_comments, | ||
| 889 | 882 | orientation=orientation, papertype=papertype, **kwargs) | |
| 890 | 883 | ||
| 891 | 884 | @_check_savefig_extra_args | |
| 892 | 885 | def _print_figure( | |
| 893 | - self, outfile, format, *, | ||
| 886 | + self, fmt, outfile, *, | ||
| 894 | 887 | dpi, dsc_comments, orientation, papertype, | |
| 895 | 888 | bbox_inches_restore=None): | |
| 896 | 889 | """ | |
@@ -900,7 +893,7 @@ def _print_figure( | |||
| 900 | 893 | all string containing Document Structuring Convention comments, | |
| 901 | 894 | generated from the *metadata* parameter to `.print_figure`. | |
| 902 | 895 | """ | |
| 903 | - is_eps = format == 'eps' | ||
| 896 | + is_eps = fmt == 'eps' | ||
| 904 | 897 | if not (isinstance(outfile, (str, os.PathLike)) | |
| 905 | 898 | or is_writable_file_like(outfile)): | |
| 906 | 899 | raise ValueError("outfile must be a path or a file-like object") | |
@@ -1028,7 +1021,7 @@ def print_figure_impl(fh): | |||
| 1028 | 1021 | ||
| 1029 | 1022 | @_check_savefig_extra_args | |
| 1030 | 1023 | def _print_figure_tex( | |
| 1031 | - self, outfile, format, *, | ||
| 1024 | + self, fmt, outfile, *, | ||
| 1032 | 1025 | dpi, dsc_comments, orientation, papertype, | |
| 1033 | 1026 | bbox_inches_restore=None): | |
| 1034 | 1027 | """ | |
@@ -1038,7 +1031,7 @@ def _print_figure_tex( | |||
| 1038 | 1031 | ||
| 1039 | 1032 | The rest of the behavior is as for `._print_figure`. | |
| 1040 | 1033 | """ | |
| 1041 | - is_eps = format == 'eps' | ||
| 1034 | + is_eps = fmt == 'eps' | ||
| 1042 | 1035 | ||
| 1043 | 1036 | width, height = self.figure.get_size_inches() | |
| 1044 | 1037 | xo = 0 | |
@@ -1121,6 +1114,9 @@ def _print_figure_tex( | |||
| 1121 | 1114 | ||
| 1122 | 1115 | _move_path_to_path_or_stream(tmpfile, outfile) | |
| 1123 | 1116 | ||
| 1117 | + print_ps = functools.partialmethod(_print_ps, "ps") | ||
| 1118 | + print_eps = functools.partialmethod(_print_ps, "eps") | ||
| 1119 | + | ||
| 1124 | 1120 | def draw(self): | |
| 1125 | 1121 | self.figure.draw_without_rendering() | |
| 1126 | 1122 | return super().draw() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -332,8 +332,7 @@ def __init__(self, bitmap, renderer): | |||
| 332 | 332 | ||
| 333 | 333 | dc, gfx_ctx = self._cache.get(bitmap, (None, None)) | |
| 334 | 334 | if dc is None: | |
| 335 | - dc = wx.MemoryDC() | ||
| 336 | - dc.SelectObject(bitmap) | ||
| 335 | + dc = wx.MemoryDC(bitmap) | ||
| 337 | 336 | gfx_ctx = wx.GraphicsContext.Create(dc) | |
| 338 | 337 | gfx_ctx._lastcliprect = None | |
| 339 | 338 | self._cache[bitmap] = dc, gfx_ctx | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,63 +27,30 @@ def draw(self, drawDC=None): | |||
| 27 | 27 | Render the figure using agg. | |
| 28 | 28 | """ | |
| 29 | 29 | FigureCanvasAgg.draw(self) | |
| 30 | - | ||
| 31 | - self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) | ||
| 30 | + self.bitmap = _rgba_to_wx_bitmap(self.get_renderer().buffer_rgba()) | ||
| 32 | 31 | self._isDrawn = True | |
| 33 | 32 | self.gui_repaint(drawDC=drawDC) | |
| 34 | 33 | ||
| 35 | 34 | def blit(self, bbox=None): | |
| 36 | 35 | # docstring inherited | |
| 36 | + bitmap = _rgba_to_wx_bitmap(self.get_renderer().buffer_rgba()) | ||
| 37 | 37 | if bbox is None: | |
| 38 | - self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) | ||
| 39 | - self.gui_repaint() | ||
| 40 | - return | ||
| 41 | - | ||
| 42 | - srcBmp = _convert_agg_to_wx_bitmap(self.get_renderer(), None) | ||
| 43 | - srcDC = wx.MemoryDC() | ||
| 44 | - srcDC.SelectObject(srcBmp) | ||
| 45 | - | ||
| 46 | - destDC = wx.MemoryDC() | ||
| 47 | - destDC.SelectObject(self.bitmap) | ||
| 48 | - | ||
| 49 | - x = int(bbox.x0) | ||
| 50 | - y = int(self.bitmap.GetHeight() - bbox.y1) | ||
| 51 | - destDC.Blit(x, y, int(bbox.width), int(bbox.height), srcDC, x, y) | ||
| 52 | - | ||
| 53 | - destDC.SelectObject(wx.NullBitmap) | ||
| 54 | - srcDC.SelectObject(wx.NullBitmap) | ||
| 38 | + self.bitmap = bitmap | ||
| 39 | + else: | ||
| 40 | + srcDC = wx.MemoryDC(bitmap) | ||
| 41 | + destDC = wx.MemoryDC(self.bitmap) | ||
| 42 | + x = int(bbox.x0) | ||
| 43 | + y = int(self.bitmap.GetHeight() - bbox.y1) | ||
| 44 | + destDC.Blit(x, y, int(bbox.width), int(bbox.height), srcDC, x, y) | ||
| 45 | + destDC.SelectObject(wx.NullBitmap) | ||
| 46 | + srcDC.SelectObject(wx.NullBitmap) | ||
| 55 | 47 | self.gui_repaint() | |
| 56 | 48 | ||
| 57 | 49 | ||
| 58 | - def _convert_agg_to_wx_bitmap(agg, bbox): | ||
| 59 | - """ | ||
| 60 | - Convert the region of the agg buffer bounded by bbox to a wx.Bitmap. If | ||
| 61 | - bbox is None, the entire buffer is converted. | ||
| 62 | - Note: agg must be a backend_agg.RendererAgg instance. | ||
| 63 | - """ | ||
| 64 | - if bbox is None: | ||
| 65 | - # agg => rgba buffer -> bitmap | ||
| 66 | - return wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height), | ||
| 67 | - agg.buffer_rgba()) | ||
| 68 | - else: | ||
| 69 | - # agg => rgba buffer -> bitmap => clipped bitmap | ||
| 70 | - srcBmp = wx.Bitmap.FromBufferRGBA(int(agg.width), int(agg.height), | ||
| 71 | - agg.buffer_rgba()) | ||
| 72 | - srcDC = wx.MemoryDC() | ||
| 73 | - srcDC.SelectObject(srcBmp) | ||
| 74 | - | ||
| 75 | - destBmp = wx.Bitmap(int(bbox.width), int(bbox.height)) | ||
| 76 | - destDC = wx.MemoryDC() | ||
| 77 | - destDC.SelectObject(destBmp) | ||
| 78 | - | ||
| 79 | - x = int(bbox.x0) | ||
| 80 | - y = int(int(agg.height) - bbox.y1) | ||
| 81 | - destDC.Blit(0, 0, int(bbox.width), int(bbox.height), srcDC, x, y) | ||
| 82 | - | ||
| 83 | - srcDC.SelectObject(wx.NullBitmap) | ||
| 84 | - destDC.SelectObject(wx.NullBitmap) | ||
| 85 | - | ||
| 86 | - return destBmp | ||
| 50 | + def _rgba_to_wx_bitmap(rgba): | ||
| 51 | + """Convert an RGBA buffer to a wx.Bitmap.""" | ||
| 52 | + h, w, _ = rgba.shape | ||
| 53 | + return wx.Bitmap.FromBufferRGBA(w, h, rgba) | ||
| 87 | 54 | ||
| 88 | 55 | ||
| 89 | 56 | @_BackendWx.export | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -168,24 +168,8 @@ class BboxConnector(Patch): | |||
| 168 | 168 | @staticmethod | |
| 169 | 169 | def get_bbox_edge_pos(bbox, loc): | |
| 170 | 170 | """ | |
| 171 | - Helper function to obtain the location of a corner of a bbox | ||
| 172 | - | ||
| 173 | - Parameters | ||
| 174 | - ---------- | ||
| 175 | - bbox : `matplotlib.transforms.Bbox` | ||
| 176 | - | ||
| 177 | - loc : {1, 2, 3, 4} | ||
| 178 | - Corner of *bbox*. Valid values are:: | ||
| 179 | - | ||
| 180 | - 'upper right' : 1, | ||
| 181 | - 'upper left' : 2, | ||
| 182 | - 'lower left' : 3, | ||
| 183 | - 'lower right' : 4 | ||
| 184 | - | ||
| 185 | - Returns | ||
| 186 | - ------- | ||
| 187 | - x, y : float | ||
| 188 | - Coordinates of the corner specified by *loc*. | ||
| 171 | + Return the ``(x, y)`` coordinates of corner *loc* of *bbox*; parameters | ||
| 172 | + behave as documented for the `.BboxConnector` constructor. | ||
| 189 | 173 | """ | |
| 190 | 174 | x0, y0, x1, y1 = bbox.extents | |
| 191 | 175 | if loc == 1: | |
@@ -200,35 +184,9 @@ def get_bbox_edge_pos(bbox, loc): | |||
| 200 | 184 | @staticmethod | |
| 201 | 185 | def connect_bbox(bbox1, bbox2, loc1, loc2=None): | |
| 202 | 186 | """ | |
| 203 | - Helper function to obtain a Path from one bbox to another. | ||
| 204 | - | ||
| 205 | - Parameters | ||
| 206 | - ---------- | ||
| 207 | - bbox1, bbox2 : `matplotlib.transforms.Bbox` | ||
| 208 | - Bounding boxes to connect. | ||
| 209 | - | ||
| 210 | - loc1 : {1, 2, 3, 4} | ||
| 211 | - Corner of *bbox1* to use. Valid values are:: | ||
| 212 | - | ||
| 213 | - 'upper right' : 1, | ||
| 214 | - 'upper left' : 2, | ||
| 215 | - 'lower left' : 3, | ||
| 216 | - 'lower right' : 4 | ||
| 217 | - | ||
| 218 | - loc2 : {1, 2, 3, 4}, optional | ||
| 219 | - Corner of *bbox2* to use. If None, defaults to *loc1*. | ||
| 220 | - Valid values are:: | ||
| 221 | - | ||
| 222 | - 'upper right' : 1, | ||
| 223 | - 'upper left' : 2, | ||
| 224 | - 'lower left' : 3, | ||
| 225 | - 'lower right' : 4 | ||
| 226 | - | ||
| 227 | - Returns | ||
| 228 | - ------- | ||
| 229 | - path : `matplotlib.path.Path` | ||
| 230 | - A line segment from the *loc1* corner of *bbox1* to the *loc2* | ||
| 231 | - corner of *bbox2*. | ||
| 187 | + Construct a `.Path` connecting corner *loc1* of *bbox1* to corner | ||
| 188 | + *loc2* of *bbox2*, where parameters behave as documented as for the | ||
| 189 | + `.BboxConnector` constructor. | ||
| 232 | 190 | """ | |
| 233 | 191 | if isinstance(bbox1, Rectangle): | |
| 234 | 192 | bbox1 = TransformedBbox(Bbox.unit(), bbox1.get_transform()) | |
@@ -250,22 +208,15 @@ def __init__(self, bbox1, bbox2, loc1, loc2=None, **kwargs): | |||
| 250 | 208 | bbox1, bbox2 : `matplotlib.transforms.Bbox` | |
| 251 | 209 | Bounding boxes to connect. | |
| 252 | 210 | ||
| 253 | - loc1 : {1, 2, 3, 4} | ||
| 254 | - Corner of *bbox1* to draw the line. Valid values are:: | ||
| 211 | + loc1, loc2 : {1, 2, 3, 4} | ||
| 212 | + Corner of *bbox1* and *bbox2* to draw the line. Valid values are:: | ||
| 255 | 213 | ||
| 256 | 214 | 'upper right' : 1, | |
| 257 | 215 | 'upper left' : 2, | |
| 258 | 216 | 'lower left' : 3, | |
| 259 | 217 | 'lower right' : 4 | |
| 260 | 218 | ||
| 261 | - loc2 : {1, 2, 3, 4}, optional | ||
| 262 | - Corner of *bbox2* to draw the line. If None, defaults to *loc1*. | ||
| 263 | - Valid values are:: | ||
| 264 | - | ||
| 265 | - 'upper right' : 1, | ||
| 266 | - 'upper left' : 2, | ||
| 267 | - 'lower left' : 3, | ||
| 268 | - 'lower right' : 4 | ||
| 219 | + *loc2* is optional and defaults to *loc1*. | ||
| 269 | 220 | ||
| 270 | 221 | **kwargs | |
| 271 | 222 | Patch properties for the line drawn. Valid arguments include: | |
@@ -308,18 +259,10 @@ def __init__(self, bbox1, bbox2, loc1a, loc2a, loc1b, loc2b, **kwargs): | |||
| 308 | 259 | bbox1, bbox2 : `matplotlib.transforms.Bbox` | |
| 309 | 260 | Bounding boxes to connect. | |
| 310 | 261 | ||
| 311 | - loc1a, loc2a : {1, 2, 3, 4} | ||
| 312 | - Corners of *bbox1* and *bbox2* to draw the first line. | ||
| 313 | - Valid values are:: | ||
| 314 | - | ||
| 315 | - 'upper right' : 1, | ||
| 316 | - 'upper left' : 2, | ||
| 317 | - 'lower left' : 3, | ||
| 318 | - 'lower right' : 4 | ||
| 319 | - | ||
| 320 | - loc1b, loc2b : {1, 2, 3, 4} | ||
| 321 | - Corners of *bbox1* and *bbox2* to draw the second line. | ||
| 322 | - Valid values are:: | ||
| 262 | + loc1a, loc2a, loc1b, loc2b : {1, 2, 3, 4} | ||
| 263 | + The first line connects corners *loc1a* of *bbox1* and *loc2a* of | ||
| 264 | + *bbox2*; the second line connects corners *loc1b* of *bbox1* and | ||
| 265 | + *loc2b* of *bbox2*. Valid values are:: | ||
| 323 | 266 | ||
| 324 | 267 | 'upper right' : 1, | |
| 325 | 268 | 'upper left' : 2, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments