- 摘抄自Duncan M. McGreggor所著《Mastering matplotlib》(Packt出版社)
Matplotlib架构
- The current matplotlib architecture revolves around the operations that are necessary for the users to create, render, and update the Figure objects.
- Figures can be displayed and interacted with via common user interface events such as the keyboard and mouse inputs. This layer of interaction with common user interface is called the backend layer.
- A Figure needs to be composed of multiple objects that should be individually modifable, but it should be implemented in such a way that it has a positive and predictable impact on the other aspects of the Figure.
- This logical layer is responsible for the abstraction of each visual component that one sees in a Figure. Due to its highly visual nature, this layer was identifed as the more general concept of creating visual art and is thus referred to as the artist layer.
- Lastly, the Figure needs to support programmatic interaction and provide the users with the ability to manipulate Figures with a syntax that is as clean and intuitive as possible. This is called the scripting layer.
Backend Layer
如图:
- the backends in matplotlib can be divided into two functional categories:
• User interface backends (interactive)
• Hardcopy backends (noninteractive)- Hardcopy backends can be further divided based on the support of raster graphics,vector graphics, or both of these.
- Furthermore, the user-interface and hardcopy backends are built upon some core abstractions. The base classes for these are as follows:
• FigureCanvasBase and FigureManagerBase
• RendererBase and GraphicsContextBase
• Event, ShowBase, and Timer.- Examining these base classes brings us to the nuts and bolts of the matplotlib backend architecture.
FigureCanvasBase
- The FigureCanvasBase class is a base class that is used by the user interface and hardcopy backends. It represents the canvas in which the Figure will render.Its responsibilities include the following:
• Holding a reference to the Figure
• Updating the Figure with a reference to the canvas
• Defning event methods that run registered
• Translating native toolkit events into the matplotlib event abstraction framework
• Defning draw methods to render the Figure
• Methods to start and stop non-GUI event loops
RendererBase
- In matplotlib, the renderer handles the drawing operations.
Event
- There are several aspects of the matplotlib backend that have to do with events,event loops, and timing. These responsibilities are divided across three base classes:
• Event: This is the base class for DrawEvent, MouseEvent, and KeyEvent, among others.
• ShowBase: This is subclassed at the module level in the GUI backends.
• TimerBase: This is the base class for TimerQT, TimerGTK3, and TimerWx, to name a few.
The artist layer
- The artist layer constitutes the bulk of what matplotlib actually does—the generation of the plots for the purpose of display, manipulation, and publication. Most work in the artist layer is performed by a number of classes, most of which are derived from the Artist base class.
- The artist layer is concerned with things such as the lines, shapes, axes, text, and so on.
- The Artist subclasses can be classifed into one of the following two groups:
• Primitives
• Containers
Primitives
- The matplotlib artist primitives are classes of graphical objects that are supposed to be painted on a fgure's canvas. These include, but are not limited to, the following:
• Line2D
• Shape (patch) classes such as Rectangle, Polygon, Ellipse, Circle, ArcText, Annotation, and TextPath
• AxesImage and FigureImage- Each of these primitive classes is a subclass of Artist, and as such have at their corethe same defnition of purpose—something that renders into an implementation of FigureCanvasBase.
Containers
- This is another set of classes that subclass from Artist and which have additional responsibilities. They offer a useful abstraction to gather primitives. Examples of the containers include the following:
• Figure
• XAxis and YAxis
• Axes, PolarAxes, HammerAxes, MollweideAxes, and LambertAxes
• Subplot- Typically, a Figure would be instantiated and used to create one or more Axes or Subplot instances. The methods available for these objects would then be used to create the primitives as needed. Thus the user does not have to manually track the creation of the primitives and store them in the appropriate containers.
- Of all the containers, the Axes class is one of the most important. It is the primary mover and shaker in the artist layer. The reason for this is simple—the Axes instances are where most of the matplotlib objects go (both primitives and other containers). In addition to the creation of primitives, the methods of this class can prepare the supplied data that is needed for the creation of primitives such as lines and shapes, add them to the appropriate containers, and draw them when called by some other objects.
- Furthermore, the Axes objects set the coordinate system for the figure and track callbacks that can be connected to the xlim_changed and ylim_changed events. The callbacks will be called with the Axes instances as an argument.
Collections
如图:
- Another component of the artist layer that we will touch on briefly is collections. These are the classes that provide for the effcient drawing of large numbers of similar objects. If you fnd yourself creating tens or hundreds of thousands of circles, polygons, lines, and so on, in most cases you will get much better performance from matplotlib if you put these in collections. The available classes include, but are not limited to PathCollection, CircleCollection, PolyCollection, EllipseCollection, LineCollection, and EventCollection.