This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Review and

In the early stage, we have learned the basic knowledge related to matplotlib module and operated the line chart, pie chart and bar chart of Matplotlib module.

As we all know, Matplotlib is a low-level library for visual data processing. There are four main steps in drawing graphs

  • Import matplotlib. Pplot library
  • The pandas/ NUMPY module is used for integer analysis of the data
  • Call the plotting method in PyPlot to draw line charts, pie charts, etc
  • A call to Pyplot.show shows this

On the Matplotlib website, you can find a variety of diagrams and tutorials

That’s what we learned last time. How does the underlying matplotlib module work?

As the saying goes, learning should be done to know, but also to know why, so as to better use the matplotlib module related methods.

Let’s go to matplotlib

1. Composition of the Matplotlib framework

The Matplotlib module can implement complex low-level operations in many data visualization libraries. Gglot, Seaborn, plotnline are all based on the matplotlib module to encapsulate different styles of statistical charts.

The bottom layer of Matplotlib module is mainly composed of script layer, art layer and back-end layer.

  • Scripting layer: Provides visual programming interfaces for users
  • Art layer: There are a number of interfaces for graphing methods
  • Back end: Interface that connects hardware and processes image elements

2. Scripting

The script layer is the top layer of the Matplotlib module and mainly provides the user with a visual programming interface, representing the PyPlot module.

For ordinary users, the PyPlot interface can be used to generate images and coordinates for most text and pass them to the back end for processing.

  • When the matplotlib.pyplot interface is imported, matplotlib.pyplot as PLT is usually imported

    • Import the PyPlot module and rename it PLT
    • When the PyPlot module loads, it analyzes the local configuration file
    • The default backend is also declared, such as creating Figure objects
    • Deeply copy the script to the back end and exit
  • The PyPlot module calls upon the matplotlib approach

    • Provides oo oriented invocation styles that show the creation of graphs and axes calling their methods
    • You rely on PyPlot to automatically create and manage graphs and axes, and use the Pyplot function for plotting
  • The user simply calls the methods associated with the Pyplot module to draw beautiful charts

    
    from matplotlib import pyplot
    
    import pandas
    
    pyplot.rcParams["font.sans-serif"] = ['SimHei']
    pyplot.rcParams["axes.unicode_minus"] =False
    
    pyplot.bar([1.2.3.4.5.6], [45.20.19.56.35.69])
    
    pyplot.title("data analyze")
    pyplot.xlabel("Elements of a")
    pyplot.ylabel(Elements of "b")
    
    pyplot.show()
    
    Copy the code

3. Artist

The art layer is located in the middle of the Matplotlib, and is mainly used for data-related drawing. Titles, lines and scales in graphs are all instances of Artist objects.

  • Artist layer characteristics

    • The Figure object created by the script layer is the Artist object instance
    • Artist’s base class is matplotlib.artist.Artist, which shares all Artist properties from Artist system to canvas coordinate system changes
    • Provides an interface for handling user interaction actions
  • Matplotlib graph object

We can see in the following Matplotlibe chart that a chart is composed of multiple objects.

  • Matplotlib diagram object description

    object instructions
    Figure Figure, popup box mouth is figure
    axes subgraph
    title The title
    legend legend
    Major tick Large scale scale
    Minor tick Small scale scale
    Line draught
    axis label Coordinate Indicator Description
    Marker Data Standard Specification
  • Artist Object Description

    ArtIst objects include Figure, Axes, and Axis objects, which are their base classes. Their ArtIst objects are all located on the canvas canvas provided by the back end.

    • Figure

      • A chart window is a figure object
      • Figure objects should contain at least one Axes object subgraph
      • Figure objects can contain Artist objects such as title and label
      • The invisible canvas contained in the figure object. Called when the image is drawn
    • Axes

      • Axes are subgraph objects, and subgraph objects refer to the X and y axes.
      • Axes used set_xlabel() and set_ylabel() to set x and Y coordinate names
    • Axis

      • Axis is an object that represents the data axis and is primarily used to represent scale positions and display values
      • Axis contains two child objects, a Locator for controlling the scale position and a display scale Formatter
    • The Artist object hierarchy is shown as follows

4. Backend

The back-end layer is mainly the low-level implementation of Matplotlib module, which mainly realizes three aspects of abstract interface 4.

  • FigureCanvas: Encapsulates the canvas functionality of Artist object drawing

    The bottom layer of matplotlib module is based on the hard user screen, and the FigureCaves interface mainly completes the initial initialization work

    • Embed itself in the native QT visual window (qtgui.qMainWindow)
    • Render matplotlib Render to Canvas (qtgui.qpainter)
    • Native Qt events are converted to matplotlib’s Event interface, which receives the information and processes it
  • Renderer: Acts as a brush that performs the drawing action

    Render mainly provides a hardware – level drawing interface to execute Artist drawing commands.

    • The Render interface was originally derived from GDK’s Drawable interface and was later converted to a native drawing command on a separate backend.
    • Matplotlib is a library of modules supporting C++ based pixel point core renderer agg
    • It can be used for 2D anti-aliasing rendering and PNG image generation
  • Event: Processes the events of keyboard and mouse input

    The Event framework maps UI events such as key-press-event or mouse-motion-Event to keyboard or mouse Event classes.

    • Users can concatenate events and use functions for callbacks
    • Graphics and data interaction

conclusion

In this issue, we will have an in-depth understanding and study of the underlying implementation of matplotlib module. In the matplotlib module, the bottom layer is based on the C++ template library Agg to render the image effects, while improving the scripting layer pyplot to make it easy for non-professionals to process the data display data.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time