backend_template.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. """
  2. A fully functional, do-nothing backend intended as a template for backend
  3. writers. It is fully functional in that you can select it as a backend e.g.
  4. with ::
  5. import matplotlib
  6. matplotlib.use("template")
  7. and your program will (should!) run without error, though no output is
  8. produced. This provides a starting point for backend writers; you can
  9. selectively implement drawing methods (`~.RendererTemplate.draw_path`,
  10. `~.RendererTemplate.draw_image`, etc.) and slowly see your figure come to life
  11. instead having to have a full-blown implementation before getting any results.
  12. Copy this file to a directory outside the Matplotlib source tree, somewhere
  13. where Python can import it (by adding the directory to your ``sys.path`` or by
  14. packaging it as a normal Python package); if the backend is importable as
  15. ``import my.backend`` you can then select it using ::
  16. import matplotlib
  17. matplotlib.use("module://my.backend")
  18. If your backend implements support for saving figures (i.e. has a `print_xyz`
  19. method), you can register it as the default handler for a given file type::
  20. from matplotlib.backend_bases import register_backend
  21. register_backend('xyz', 'my_backend', 'XYZ File Format')
  22. ...
  23. plt.savefig("figure.xyz")
  24. """
  25. from matplotlib import _api
  26. from matplotlib._pylab_helpers import Gcf
  27. from matplotlib.backend_bases import (
  28. FigureCanvasBase, FigureManagerBase, GraphicsContextBase, RendererBase)
  29. from matplotlib.figure import Figure
  30. class RendererTemplate(RendererBase):
  31. """
  32. The renderer handles drawing/rendering operations.
  33. This is a minimal do-nothing class that can be used to get started when
  34. writing a new backend. Refer to `.backend_bases.RendererBase` for
  35. documentation of the methods.
  36. """
  37. def __init__(self, dpi):
  38. super().__init__()
  39. self.dpi = dpi
  40. def draw_path(self, gc, path, transform, rgbFace=None):
  41. pass
  42. # draw_markers is optional, and we get more correct relative
  43. # timings by leaving it out. backend implementers concerned with
  44. # performance will probably want to implement it
  45. # def draw_markers(self, gc, marker_path, marker_trans, path, trans,
  46. # rgbFace=None):
  47. # pass
  48. # draw_path_collection is optional, and we get more correct
  49. # relative timings by leaving it out. backend implementers concerned with
  50. # performance will probably want to implement it
  51. # def draw_path_collection(self, gc, master_transform, paths,
  52. # all_transforms, offsets, offset_trans,
  53. # facecolors, edgecolors, linewidths, linestyles,
  54. # antialiaseds):
  55. # pass
  56. # draw_quad_mesh is optional, and we get more correct
  57. # relative timings by leaving it out. backend implementers concerned with
  58. # performance will probably want to implement it
  59. # def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
  60. # coordinates, offsets, offsetTrans, facecolors,
  61. # antialiased, edgecolors):
  62. # pass
  63. def draw_image(self, gc, x, y, im):
  64. pass
  65. def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
  66. pass
  67. def flipy(self):
  68. # docstring inherited
  69. return True
  70. def get_canvas_width_height(self):
  71. # docstring inherited
  72. return 100, 100
  73. def get_text_width_height_descent(self, s, prop, ismath):
  74. return 1, 1, 1
  75. def new_gc(self):
  76. # docstring inherited
  77. return GraphicsContextTemplate()
  78. def points_to_pixels(self, points):
  79. # if backend doesn't have dpi, e.g., postscript or svg
  80. return points
  81. # elif backend assumes a value for pixels_per_inch
  82. # return points/72.0 * self.dpi.get() * pixels_per_inch/72.0
  83. # else
  84. # return points/72.0 * self.dpi.get()
  85. class GraphicsContextTemplate(GraphicsContextBase):
  86. """
  87. The graphics context provides the color, line styles, etc. See the cairo
  88. and postscript backends for examples of mapping the graphics context
  89. attributes (cap styles, join styles, line widths, colors) to a particular
  90. backend. In cairo this is done by wrapping a cairo.Context object and
  91. forwarding the appropriate calls to it using a dictionary mapping styles
  92. to gdk constants. In Postscript, all the work is done by the renderer,
  93. mapping line styles to postscript calls.
  94. If it's more appropriate to do the mapping at the renderer level (as in
  95. the postscript backend), you don't need to override any of the GC methods.
  96. If it's more appropriate to wrap an instance (as in the cairo backend) and
  97. do the mapping here, you'll need to override several of the setter
  98. methods.
  99. The base GraphicsContext stores colors as an RGB tuple on the unit
  100. interval, e.g., (0.5, 0.0, 1.0). You may need to map this to colors
  101. appropriate for your backend.
  102. """
  103. ########################################################################
  104. #
  105. # The following functions and classes are for pyplot and implement
  106. # window/figure managers, etc.
  107. #
  108. ########################################################################
  109. class FigureManagerTemplate(FigureManagerBase):
  110. """
  111. Helper class for pyplot mode, wraps everything up into a neat bundle.
  112. For non-interactive backends, the base class is sufficient. For
  113. interactive backends, see the documentation of the `.FigureManagerBase`
  114. class for the list of methods that can/should be overridden.
  115. """
  116. class FigureCanvasTemplate(FigureCanvasBase):
  117. """
  118. The canvas the figure renders into. Calls the draw and print fig
  119. methods, creates the renderers, etc.
  120. Note: GUI templates will want to connect events for button presses,
  121. mouse movements and key presses to functions that call the base
  122. class methods button_press_event, button_release_event,
  123. motion_notify_event, key_press_event, and key_release_event. See the
  124. implementations of the interactive backends for examples.
  125. Attributes
  126. ----------
  127. figure : `~matplotlib.figure.Figure`
  128. A high-level Figure instance
  129. """
  130. # The instantiated manager class. For further customization,
  131. # ``FigureManager.create_with_canvas`` can also be overridden; see the
  132. # wx-based backends for an example.
  133. manager_class = FigureManagerTemplate
  134. def draw(self):
  135. """
  136. Draw the figure using the renderer.
  137. It is important that this method actually walk the artist tree
  138. even if not output is produced because this will trigger
  139. deferred work (like computing limits auto-limits and tick
  140. values) that users may want access to before saving to disk.
  141. """
  142. renderer = RendererTemplate(self.figure.dpi)
  143. self.figure.draw(renderer)
  144. # You should provide a print_xxx function for every file format
  145. # you can write.
  146. # If the file type is not in the base set of filetypes,
  147. # you should add it to the class-scope filetypes dictionary as follows:
  148. filetypes = {**FigureCanvasBase.filetypes, 'foo': 'My magic Foo format'}
  149. def print_foo(self, filename, **kwargs):
  150. """
  151. Write out format foo.
  152. This method is normally called via `.Figure.savefig` and
  153. `.FigureCanvasBase.print_figure`, which take care of setting the figure
  154. facecolor, edgecolor, and dpi to the desired output values, and will
  155. restore them to the original values. Therefore, `print_foo` does not
  156. need to handle these settings.
  157. """
  158. self.draw()
  159. def get_default_filetype(self):
  160. return 'foo'
  161. ########################################################################
  162. #
  163. # Now just provide the standard names that backend.__init__ is expecting
  164. #
  165. ########################################################################
  166. FigureCanvas = FigureCanvasTemplate
  167. FigureManager = FigureManagerTemplate