1
1
"""
2
- This is a fully functional do nothing backend to provide a template to
3
- backend writers. It is fully functional in that you can select it as
4
- a backend with
5
-
6
- import matplotlib
7
- matplotlib.use('Template')
8
-
9
- and your matplotlib scripts will (should!) run without error, though
10
- no output is produced. This provides a nice starting point for
11
- backend writers because you can selectively implement methods
12
- (draw_rectangle, draw_lines, etc...) and slowly see your figure come
13
- to life w/o having to have a full blown implementation before getting
14
- any results.
15
-
16
- Copy this to backend_xxx.py and replace all instances of 'template'
17
- with 'xxx'. Then implement the class methods and functions below, and
18
- add 'xxx' to the switchyard in matplotlib/backends/__init__.py and
19
- 'xxx' to the backends list in the validate_backend method in
20
- matplotlib/__init__.py and you're off. You can use your backend with::
21
-
22
- import matplotlib
23
- matplotlib.use('xxx')
24
- import matplotlib.pyplot as plt
25
- plt.plot([1,2,3])
26
- plt.show()
27
-
28
- matplotlib also supports external backends, so you can place you can
29
- use any module in your PYTHONPATH with the syntax::
30
-
31
- import matplotlib
32
- matplotlib.use('module://my_backend')
33
-
34
- where my_backend.py is your module name. This syntax is also
35
- recognized in the rc file and in the -d argument in pylab, e.g.,::
36
-
37
- python simple_plot.py -dmodule://my_backend
38
-
39
- If your backend implements support for saving figures (i.e. has a print_xyz()
40
- method) you can register it as the default handler for a given file type
41
-
42
- from matplotlib.backend_bases import register_backend
43
- register_backend('xyz', 'my_backend', 'XYZ File Format')
44
- ...
45
- plt.savefig("figure.xyz")
46
-
47
- The files that are most relevant to backend_writers are
48
-
49
- matplotlib/backends/backend_your_backend.py
50
- matplotlib/backend_bases.py
51
- matplotlib/backends/__init__.py
52
- matplotlib/__init__.py
53
- matplotlib/_pylab_helpers.py
54
-
55
- Naming Conventions
56
-
57
- * classes Upper or MixedUpperCase
58
-
59
- * variables lower or lowerUpper
60
-
61
- * functions lower or underscore_separated
62
-
2
+ This is a fully functional do nothing backend to provide a template to backend
3
+ writers. It is fully functional in that you can select it as a backend e.g.
4
+ with ::
5
+
6
+ import matplotlib
7
+ matplotlib.use("template")
8
+
9
+ and your program will (should!) run without error, though no output is
10
+ produced. This provides a starting point for backend writers; you can
11
+ selectively implement drawing methods (`draw_path`, `draw_image`, etc.) and
12
+ slowly see your figure come to life instead having to have a full blown
13
+ implementation before getting any results.
14
+
15
+ Copy this file to a directory outside of the Matplotlib source tree, somewhere
16
+ where Python can import it (by adding the directory to your ``sys.path`` or by
17
+ packaging it as a normal Python package); if the backend is importable as
18
+ ``import my.backend`` you can then select it using ::
19
+
20
+ import matplotlib
21
+ matplotlib.use("module://my.backend")
22
+
23
+ If your backend implements support for saving figures (i.e. has a `print_xyz`
24
+ method), you can register it as the default handler for a given file type::
25
+
26
+ from matplotlib.backend_bases import register_backend
27
+ register_backend('xyz', 'my_backend', 'XYZ File Format')
28
+ ...
29
+ plt.savefig("figure.xyz")
63
30
"""
64
31
65
32
from matplotlib ._pylab_helpers import Gcf
@@ -73,10 +40,12 @@ class RendererTemplate(RendererBase):
73
40
The renderer handles drawing/rendering operations.
74
41
75
42
This is a minimal do-nothing class that can be used to get started when
76
- writing a new backend. Refer to backend_bases.RendererBase for
77
- documentation of the classes methods.
43
+ writing a new backend. Refer to ` backend_bases.RendererBase` for
44
+ documentation of the methods.
78
45
"""
46
+
79
47
def __init__ (self , dpi ):
48
+ super ().__init__ ()
80
49
self .dpi = dpi
81
50
82
51
def draw_path (self , gc , path , transform , rgbFace = None ):
@@ -160,11 +129,12 @@ class GraphicsContextTemplate(GraphicsContextBase):
160
129
161
130
########################################################################
162
131
#
163
- # The following functions and classes are for pylab and implement
132
+ # The following functions and classes are for pyplot and implement
164
133
# window/figure managers, etc...
165
134
#
166
135
########################################################################
167
136
137
+
168
138
def draw_if_interactive ():
169
139
"""
170
140
For image backends - is not required.
@@ -186,9 +156,7 @@ def show(*, block=None):
186
156
187
157
188
158
def new_figure_manager (num , * args , FigureClass = Figure , ** kwargs ):
189
- """
190
- Create a new figure manager instance
191
- """
159
+ """Create a new figure manager instance."""
192
160
# If a main-level app must be created, this (and
193
161
# new_figure_manager_given_figure) is the usual place to do it -- see
194
162
# backend_wx, backend_wxagg and backend_tkagg for examples. Not all GUIs
@@ -199,9 +167,7 @@ def new_figure_manager(num, *args, FigureClass=Figure, **kwargs):
199
167
200
168
201
169
def new_figure_manager_given_figure (num , figure ):
202
- """
203
- Create a new figure manager instance for the given figure.
204
- """
170
+ """Create a new figure manager instance for the given figure."""
205
171
canvas = FigureCanvasTemplate (figure )
206
172
manager = FigureManagerTemplate (canvas , num )
207
173
return manager
@@ -225,9 +191,7 @@ class methods button_press_event, button_release_event,
225
191
"""
226
192
227
193
def draw (self ):
228
- """
229
- Draw the figure using the renderer
230
- """
194
+ """Draw the figure using the renderer."""
231
195
renderer = RendererTemplate (self .figure .dpi )
232
196
self .figure .draw (renderer )
233
197
@@ -245,19 +209,18 @@ def print_foo(self, filename, *args, **kwargs):
245
209
to their original values after this call, so you don't need to
246
210
save and restore them.
247
211
"""
248
- pass
249
212
250
213
def get_default_filetype (self ):
251
214
return 'foo'
EED3
252
215
253
216
254
217
class FigureManagerTemplate (FigureManagerBase ):
255
218
"""
256
- Wrap everything up into a window for the pylab interface
219
+ Helper class for pyplot mode, wraps everything up into a neat bundle.
257
220
258
- For non interactive backends, the base class does all the work
221
+ For non- interactive backends, the base class is sufficient.
259
222
"""
260
- pass
223
+
261
224
262
225
########################################################################
263
226
#
0 commit comments