1
- from matplotlib .backend_bases import RendererBase
1
+ from typing import Tuple , Union
2
+ from matplotlib .axes import Axes
3
+ from matplotlib .backend_bases import RendererBase , GraphicsContextBase
4
+ from matplotlib .font_manager import FontProperties
2
5
from matplotlib .patches import Rectangle
6
+ from matplotlib .texmanager import TexManager
3
7
from matplotlib .transforms import Bbox , IdentityTransform , Affine2D , \
4
- TransformedPatchPath
8
+ TransformedPatchPath , Transform
5
9
from matplotlib .path import Path
6
10
import matplotlib ._image as _image
7
11
import numpy as np
8
12
from matplotlib .image import _interpd_
9
13
from matplotview ._docs import dynamic_doc_string , get_interpolation_list_str
10
14
15
+ ColorTup = Union [None , Tuple [float , float , float , float ], Tuple [float , float , float ]]
11
16
12
17
class _TransformRenderer (RendererBase ):
13
18
"""
@@ -19,12 +24,12 @@ class _TransformRenderer(RendererBase):
19
24
@dynamic_doc_string (interp_list = get_interpolation_list_str ())
20
25
def __init__ (
21
26
self ,
22
- base_renderer ,
23
- mock_transform ,
24
- transform ,
25
- bounding_axes ,
26
- image_interpolation = "nearest" ,
27
- scale_linewidths = True
27
+ base_renderer : RendererBase ,
28
+ mock_transform : Transform ,
29
+ transform : Transform ,
30
+ bounding_axes : Axes ,
31
+ image_interpolation : str = "nearest" ,
32
+ scale_linewidths : bool = True
28
33
):
29
34
"""
30
35
Constructs a new TransformRender.
@@ -80,10 +85,10 @@ def __init__(
80
85
)
81
86
82
87
@property
83
- def bounding_axes (self ):
88
+ def bounding_axes (self ) -> Axes :
84
89
return self .__bounding_axes
85
90
86
- def _scale_gc (self , gc ) :
91
+ def _scale_gc (self , gc : GraphicsContextBase ) -> GraphicsContextBase :
87
92
with np .errstate (all = 'ignore' ):
88
93
transfer_transform = self ._get_transfer_transform (
89
94
IdentityTransform ()
@@ -103,14 +108,14 @@ def _scale_gc(self, gc):
103
108
104
109
return new_gc
105
110
106
- def _get_axes_display_box (self ):
111
+ def _get_axes_display_box (self ) -> Bbox :
107
112
"""
108
113
Private method, get the bounding box of the child axes in display
109
114
coordinates.
110
115
"""
111
116
return self .__bounding_axes .get_window_extent ()
112
117
113
- def _get_transfer_transform (self , orig_transform ) :
118
+ def _get_transfer_transform (self , orig_transform : Transform ) -> Transform :
114
119
"""
115
120
Private method, returns the transform which translates and scales
116
121
coordinates as if they were originally plotted on the child axes
@@ -141,43 +146,63 @@ def _get_transfer_transform(self, orig_transform):
141
146
# We copy all of the properties of the renderer we are mocking, so that
142
147
# artists plot themselves as if they were placed on the original renderer.
143
148
@property
144
- def height (self ):
149
+ def height (self ) -> int :
145
150
return self .__renderer .get_canvas_width_height ()[1 ]
146
151
147
152
@property
148
- def width (self ):
153
+ def width (self ) -> int :
149
154
return self .__renderer .get_canvas_width_height ()[0 ]
150
155
151
- def get_text_width_height_descent (self , s , prop , ismath ):
156
+ def get_text_width_height_descent (
157
+ self ,
158
+ s : str ,
159
+ prop : FontProperties ,
160
+ ismath : bool
161
+ ) -> Tuple [float , float , float ]:
152
162
return self .__renderer .get_text_width_height_descent (s , prop , ismath )
153
163
154
- def get_canvas_width_height (self ):
164
+ def get_canvas_width_height (self ) -> Tuple [ float , float ] :
155
165
return self .__renderer .get_canvas_width_height ()
156
166
157
- def get_texmanager (self ):
167
+ def get_texmanager (self ) -> TexManager :
158
168
return self .__renderer .get_texmanager ()
159
169
160
- def get_image_magnification (self ):
170
+ def get_image_magnification (self ) -> float :
161
171
return self .__renderer .get_image_magnification ()
162
172
163
- def _get_text_path_transform (self , x , y , s , prop , angle , ismath ):
164
- return self .__renderer ._get_text_path_transform (x , y , s , prop , angle ,
165
- ismath )
173
+ def _get_text_path_transform (
174
+ self ,
175
+ x : float ,
176
+ y : float ,
177
+ s : str ,
178
+ prop : FontProperties ,
179
+ angle : float ,
180
+ ismath : bool
181
+ ) -> Transform :
182
+ return self .__renderer ._get_text_path_transform (
183
+ x , y , s , prop , angle , ismath
184
+ )
166
185
167
- def option_scale_image (self ):
186
+ def option_scale_image (self ) -> bool :
168
187
return False
169
188
170
- def points_to_pixels (self , points ) :
189
+ def points_to_pixels (self , points : float ) -> float :
171
190
return self .__renderer .points_to_pixels (points )
172
191
173
- def flipy (self ):
192
+ def flipy (self ) -> bool :
174
193
return self .__renderer .flipy ()
175
194
176
- def new_gc (self ):
195
+ def new_gc (self ) -> GraphicsContextBase :
177
196
return self .__renderer .new_gc ()
178
197
179
198
# Actual drawing methods below:
180
- def draw_path (self , gc , path , transform , rgbFace = None ):
199
+ def draw_path (
200
+ self ,
201
+ gc : GraphicsContextBase ,
202
+ path : Path ,
203
+ transform : Transform ,
204
+ rgbFace : ColorTup = None
205
+ ):
181
206
# Convert the path to display coordinates, but if it was originally
182
207
# drawn on the child axes.
183
208
path = path .deepcopy ()
@@ -203,7 +228,16 @@ def draw_path(self, gc, path, transform, rgbFace=None):
203
228
204
229
self .__renderer .draw_path (gc , path , IdentityTransform (), rgbFace )
205
230
206
- def _draw_text_as_path (self , gc , x , y , s , prop , angle , ismath ):
231
+ def _draw_text_as_path (
232
+ self ,
233
+ gc : GraphicsContextBase ,
234
+ x : float ,
235
+ y : float ,
236
+ s : str ,
237
+ prop : FontProperties ,
238
+ angle : float ,
239
+ ismath : bool
240
+ ):
207
241
# If the text field is empty, don't even try rendering it...
208
242
if ((s is None ) or (s .strip () == "" )):
209
243
return
@@ -212,7 +246,13 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):
212
246
# checked above... (Above case causes error)
213
247
super ()._draw_text_as_path (gc , x , y , s , prop , angle , ismath )
214
248
215
- def draw_gouraud_triangle (self , gc , points , colors , transform ):
249
+ def draw_gouraud_triangle (
250
+ self ,
251
+ gc : GraphicsContextBase ,
252
+ points : np .ndarray ,
253
+ colors : np .ndarray ,
254
+ transform : Transform
255
+ ):
216
256
# Pretty much identical to draw_path, transform the points and adjust
217
257
# clip to the child axes bounding box.
218
258
points = self ._get_transfer_transform (transform ).transform (points )
@@ -233,7 +273,14 @@ def draw_gouraud_triangle(self, gc, points, colors, transform):
233
273
IdentityTransform ())
234
274
235
275
# Images prove to be especially messy to deal with...
236
- def draw_image (self , gc , x , y , im , transform = None ):
276
+ def draw_image (
277
+ self ,
278
+ gc : GraphicsContextBase ,
279
+ x : float ,
280
+ y : float ,
281
+ im : np .ndarray ,
282
+ transform : Transform = None
283
+ ):
237
284
mag = self .get_image_magnification ()
238
285
shift_data_transform = self ._get_transfer_transform (
239
286
IdentityTransform ()
0 commit comments