Matplotlib | Matplotlib architecture overview

Less is more effective
Less is more attractive
Less is more impactive

Here is a note about the Matplotlib architecture, mainly the content of the class plus the information you find yourself, in the English folder, you can use it as a reference.

Matplotlib architecture

for more info

The Matplotlib architecture is divided into three layers, which can be viewed as a stack. Each layer above another layer knows how to communicate with the layer below it, but the layer below it does not know the layer above it. The three layers from bottom to top are: Backend, Artist, Scripting Layer.

Backend Layer (FigureCanvas, Renderer, Event)

Has three built-in abstract interface classes:

  • FigureCanvas: matplotlib.backened_bases.FigureCnvas
    • Encompasses the area onto which the figure is drawn
    • Such as drawing paper
  • Renderer: matplotlib.backened_bases.Renderer
    • Knows how to draw on the FigureCanvas
    • E.g. brush
  • Event: matplotlib.backend_bases.Event
    • Handles user inputs such as keyboard strokes and mouse clicks

Artist Layer (Artist)

  • Comprised of one main object - Artist:
    • Knows how to use the Renderer to draw on the canvas.
    • Everything you see in matplotlib Figure is an instance of Artist.
  • Title, lines, tick labels, and images, all correspond to individual Artist instances.
  • Two types of Artist objects:
    • Primitive: Line2D, Rectangle, Circle, and Text
    • Composite: Axis, Tick, Axes, and Figure
  • Each artist may contain other composite artists as well as primitive artists.

An example of drawing with Artist~

# Putting the Artist Layer to Use
# generate a histogram of some data using the Artist layer
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas # import FigureCanvas
from matplotlib.figure import Figure # import Figure artist
fig = Figure()
canvas = FigureCanvas(fig)

# create 10000 random numbers using numpy
import numpy as np
x = np.random.randn(10000)

ax = fig.add_subplot(111) # create an axes artist

ax.hist(x, 100) # generate a histgram of the 10000 numbers

# add a little to the figure and save it
ax.set_title('Normal distribution with $\mu=0, \sigma=1$')
fig.savefig('matplotlib_histogram.png')

Insert picture description here

Scripting Layer (pyplot)

  • Daily use, more concise
  • Comprised mainly of pyplot, a scripting interface that is lighter that the Artist layer.
  • Let’s see how we can generate the same histogram of 10000 random values using the pyplot interface
import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(10000)

plt.hist(x, 100)
plt.title(r'Normal distribution with $\mu=0, \sigma=1$')
plt.savefig('matplotlib_histogram.png')
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43360896/article/details/113001344