backend_template.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. """
  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. 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 (`draw_path`, `draw_image`, etc.) and
  10. slowly see your figure come to life instead having to have a full blown
  11. implementation before getting any results.
  12. Copy this file to a directory outside of 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._pylab_helpers import Gcf
  26. from matplotlib.backend_bases import (
  27. FigureCanvasBase, FigureManagerBase, GraphicsContextBase, RendererBase)
  28. from matplotlib.figure import Figure
  29. class RendererTemplate(RendererBase):
  30. """
  31. The renderer handles drawing/rendering operations.
  32. This is a minimal do-nothing class that can be used to get started when
  33. writing a new backend. Refer to `backend_bases.RendererBase` for
  34. documentation of the methods.
  35. """
  36. def __init__(self, dpi):
  37. super().__init__()
  38. self.dpi = dpi
  39. def draw_path(self, gc, path, transform, rgbFace=None):
  40. pass
  41. # draw_markers is optional, and we get more correct relative
  42. # timings by leaving it out. backend implementers concerned with
  43. # performance will probably want to implement it
  44. # def draw_markers(self, gc, marker_path, marker_trans, path, trans,
  45. # rgbFace=None):
  46. # pass
  47. # draw_path_collection is optional, and we get more correct
  48. # relative timings by leaving it out. backend implementers concerned with
  49. # performance will probably want to implement it
  50. # def draw_path_collection(self, gc, master_transform, paths,
  51. # all_transforms, offsets, offsetTrans,
  52. # facecolors, edgecolors, linewidths, linestyles,
  53. # antialiaseds):
  54. # pass
  55. # draw_quad_mesh is optional, and we get more correct
  56. # relative timings by leaving it out. backend implementers concerned with
  57. # performance will probably want to implement it
  58. # def draw_quad_mesh(self, gc, master_transform, meshWidth, meshHeight,
  59. # coordinates, offsets, offsetTrans, facecolors,
  60. # antialiased, edgecolors):
  61. # pass
  62. def draw_image(self, gc, x, y, im):
  63. pass
  64. def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
  65. pass
  66. def flipy(self):
  67. # docstring inherited
  68. return True
  69. def get_canvas_width_height(self):
  70. # docstring inherited
  71. return 100, 100
  72. def get_text_width_height_descent(self, s, prop, ismath):
  73. return 1, 1, 1
  74. def new_gc(self):
  75. # docstring inherited
  76. return GraphicsContextTemplate()
  77. def points_to_pixels(self, points):
  78. # if backend doesn't have dpi, e.g., postscript or svg
  79. return points
  80. # elif backend assumes a value for pixels_per_inch
  81. #return points/72.0 * self.dpi.get() * pixels_per_inch/72.0
  82. # else
  83. #return points/72.0 * self.dpi.get()
  84. class GraphicsContextTemplate(GraphicsContextBase):
  85. """
  86. The graphics context provides the color, line styles, etc... See the cairo
  87. and postscript backends for examples of mapping the graphics context
  88. attributes (cap styles, join styles, line widths, colors) to a particular
  89. backend. In cairo this is done by wrapping a cairo.Context object and
  90. forwarding the appropriate calls to it using a dictionary mapping styles
  91. to gdk constants. In Postscript, all the work is done by the renderer,
  92. mapping line styles to postscript calls.
  93. If it's more appropriate to do the mapping at the renderer level (as in
  94. the postscript backend), you don't need to override any of the GC methods.
  95. If it's more appropriate to wrap an instance (as in the cairo backend) and
  96. do the mapping here, you'll need to override several of the setter
  97. methods.
  98. The base GraphicsContext stores colors as a RGB tuple on the unit
  99. interval, e.g., (0.5, 0.0, 1.0). You may need to map this to colors
  100. appropriate for your backend.
  101. """
  102. ########################################################################
  103. #
  104. # The following functions and classes are for pyplot and implement
  105. # window/figure managers, etc...
  106. #
  107. ########################################################################
  108. def draw_if_interactive():
  109. """
  110. For image backends - is not required.
  111. For GUI backends - this should be overridden if drawing should be done in
  112. interactive python mode.
  113. """
  114. def show(*, block=None):
  115. """
  116. For image backends - is not required.
  117. For GUI backends - show() is usually the last line of a pyplot script and
  118. tells the backend that it is time to draw. In interactive mode, this
  119. should do nothing.
  120. """
  121. for manager in Gcf.get_all_fig_managers():
  122. # do something to display the GUI
  123. pass
  124. def new_figure_manager(num, *args, FigureClass=Figure, **kwargs):
  125. """Create a new figure manager instance."""
  126. # If a main-level app must be created, this (and
  127. # new_figure_manager_given_figure) is the usual place to do it -- see
  128. # backend_wx, backend_wxagg and backend_tkagg for examples. Not all GUIs
  129. # require explicit instantiation of a main-level app (e.g., backend_gtk3)
  130. # for pylab.
  131. thisFig = FigureClass(*args, **kwargs)
  132. return new_figure_manager_given_figure(num, thisFig)
  133. def new_figure_manager_given_figure(num, figure):
  134. """Create a new figure manager instance for the given figure."""
  135. canvas = FigureCanvasTemplate(figure)
  136. manager = FigureManagerTemplate(canvas, num)
  137. return manager
  138. class FigureCanvasTemplate(FigureCanvasBase):
  139. """
  140. The canvas the figure renders into. Calls the draw and print fig
  141. methods, creates the renderers, etc.
  142. Note: GUI templates will want to connect events for button presses,
  143. mouse movements and key presses to functions that call the base
  144. class methods button_press_event, button_release_event,
  145. motion_notify_event, key_press_event, and key_release_event. See the
  146. implementations of the interactive backends for examples.
  147. Attributes
  148. ----------
  149. figure : `matplotlib.figure.Figure`
  150. A high-level Figure instance
  151. """
  152. def draw(self):
  153. """Draw the figure using the renderer."""
  154. renderer = RendererTemplate(self.figure.dpi)
  155. self.figure.draw(renderer)
  156. # You should provide a print_xxx function for every file format
  157. # you can write.
  158. # If the file type is not in the base set of filetypes,
  159. # you should add it to the class-scope filetypes dictionary as follows:
  160. filetypes = FigureCanvasBase.filetypes.copy()
  161. filetypes['foo'] = 'My magic Foo format'
  162. def print_foo(self, filename, *args, **kwargs):
  163. """
  164. Write out format foo. The dpi, facecolor and edgecolor are restored
  165. to their original values after this call, so you don't need to
  166. save and restore them.
  167. """
  168. def get_default_filetype(self):
  169. return 'foo'
  170. class FigureManagerTemplate(FigureManagerBase):
  171. """
  172. Helper class for pyplot mode, wraps everything up into a neat bundle.
  173. For non-interactive backends, the base class is sufficient.
  174. """
  175. ########################################################################
  176. #
  177. # Now just provide the standard names that backend.__init__ is expecting
  178. #
  179. ########################################################################
  180. FigureCanvas = FigureCanvasTemplate
  181. FigureManager = FigureManagerTemplate