SoFunction
Updated on 2024-11-10

Detailed explanation of plotting with the matplotlib module in Python

1, the father of matplotlib introduction

matplotlib father John D. Hunter has died, his life is brilliant and short, but he developed the open source library continues to be brilliant. Domestic introduction of the information is too little, access to some of the organization is as follows:

  • 1968 Born in Dyersburg, Tennessee, USA.
  • He then studied at Princeton University.
  • Matplotlib version 0.1 was released in 2003, initially intended for visualizing electroencephalogram (ECoG) data from epileptic patients during PhD studies;
  • Later, the team behind the Hubble Space Telescope at NASA's Space Telescope Science Institute chose Matplotlib as its drawing package and has been providing financial support to the Matplotlib development team, which has greatly contributed to the development of Matplotlib.
  • D. in Neurobiology from the University of Chicago in 2004.
  • Worked for an investment firm in Chicago in 2005 doing quantitative analysis (really biologists in all walks of life).
  • He then founded the NumFOCUS Foundation, a non-profit organization dedicated to data science, as a board member.
  • NASA used Matplotlib to visualize the first images of black holes when the U.S. Phoenix spacecraft landed on Mars in 2007.
  • In 2012, John D. Hunter was honored by the Python community for his outstanding personal contributions to the direction of Python and data science in the firstPSF Distinguished Service Awardsawards  。
  • 2012.08 Died prematurely of malignant colon cancer at the age of 44.
  • Although the father of Matplotlib has passed away, a large number of open source enthusiasts are forking over this visualization package to continue the glory of/matplotlib
  • The NumFOCUS organization sponsors 1 or 2 students each summer to work full-time for Matplolib for about 10 weeks under the leadership of a Senior Contributor (up to $6,000 in 2018 awards):/programs/john-hunter-technology-fellowship
  • Since 2013, SciPy has held an annual visualization competition, the John Hunter Excellence in Plotting Competition, to honor John Hunter's contributions and to highlight the importance of data visualization to the advancement of science and to demonstrate the power of open source software. The entry deadline for 2020 is June 01, and the prizes are generous (1st prize: $1000; 2nd prize: $750; 3rd prize: $500):/

2, matplotlib graphic structure

figure layer

Refers to the whole picture, you can set the resolution of the whole picture (dpi), length and width (figsize), title (title) and other characteristics;

can contain multiple axes, which can be simply understood as multiple subgraphs (two axes in the figure below);

figure is placed on top of the canvas system layer and is not visible to the user.

axes layer

For each subgraph, various graphs can be plotted, such as bar charts (bar), pie charts (pie function), box plots (boxplot), and so on;

Setting switches for the appearance of grid lines, axis switches, etc. for each graph;

Sets the name (label), subfigure title, legend, etc. for each axis;

Sets the axis range (scale), axis ticks, and so on;

The figure below has two axes:

Composition of a matplotlib graph

The following matplotlib diagram contains the common elements of a graph, such as title, axes, axis labels, scale, text comments, legend, and so on.

3, matplotlib two kinds of drawing drawing methods

Method 1: Use

synopsis

This kind of plotting mainly uses the pyplot module, with more than 3,000 lines of code (stored in xxx\site-packages\matplotlib\ under windows), the script has a large number of def-defined functions inside, and the plotting is to call the functions.

Example of pyplot method plotting

# Interfaces
import numpy as np
import  as plt# Import pyplot, abbreviated as plt
def f(t):
    return (-t) * (2**t)

t1 = (0.0, 5.0, 0.1)
t2 = (0.0, 5.0, 0.02)

(dpi=100)
(211)
(t1, f(t1), color='tab:blue', marker='o')
(t2, f(t2), color='black')
('demo')

(212)
(t2, (2**t2), color='tab:orange', linestyle='--')
(' api')
()

Approach II: Object-oriented approach

The object-oriented approach is more convenient when drawing more complex shapes. This way of drawing the main use of matplotlib's two subclasses: and, when drawing each map, the canvas for an instance, each sub-diagram for an instance, respectively, can inherit all the methods of the parent class, that is to say, when you draw, you want to set up the elements (grid lines, ah, coordinates, ah, etc.) can be in the attributes of the two can be found in the use of it.

This object is mainly used for figure adjustments

Examples of Object-Oriented Methods Mapping

import numpy as np
import  as plt

def f(t):
    return (-t) * (2**t)


t1 = (0.0, 5.0, 0.1)
t2 = (0.0, 5.0, 0.02)

fig, axs = (2, dpi=100)
#fig is an instance of the object figure
#axs for the object instances (each subgraph) that comprise the
axs[0].plot(t1, f(t1), color='tab:blue', marker='o')
axs[0].plot(t2, f(t2), color='black')

# Two ways to set a title
#axs[0].set_title('haha')#Set the subheading using the set_title method.
axs[0].set(title='demo1')

axs[1].plot(t2, (2**t2), color='tab:orange', linestyle='--')
('matplotlib object-oriented')The suptitle method in #Usage sets the Figure title.
()

The above is a detailed explanation of the plotting method of the matplotlib module in Python, more information about Python matplotlib plotting method, please pay attention to my other related articles!