Wechat official account: “Python reading money” if you have any questions or suggestions, please leave a message

I’m going to write a series of Matplotlib tutorials about data visualization for Pandas. Those of you who have read Pandas will know that my writing will incorporate more of my understanding of the subject, as will the Matplotlib series. This series will cover some simple concepts of Matplotlib, drawing principles, common graphics, and advanced drawing techniques. Once you’re done, you want to be able to draw something like this using Matplotlib.

This article will introduce some basic concepts and drawing principles of Matplotlib and get down to business

I wonder how many of you, like me, were confused by the concepts of PLT, AX and subplots in the book when you first came to Matplotlib. Don’t they draw the same picture? What’s the difference? Here’s a step-by-step guide to these puzzles.

Concept is introduced

First, we should look at the structure of a Matplotlib diagram, quoting an official diagram:

Let’s focus on the Figure in the red box and Axes in the blue box. How do we understand these two things?

If you compare the Matplotlib plots to our usual ones, Figure would be a piece of paper (usually called a canvas), while Axes would represent an area on the paper (there could be multiple areas, which is the subplots we’ll talk about later), and a more graphic map from the plots.

In the Figure canvas, a data dashboard is drawn in the Axes1 area, a bar chart is drawn in the Axes2 area, and a map is drawn in the Axes3 area.

The two drawing methods are different

With a basic understanding of both concepts, it’s time to look at the differences between plt.plot() and ax.plot(). Here are two ways to plot a chart using Matplotlib.

  • plt
# First way
plt.figure()
plt.plot([1.2.3], [4.5.6])
plt.show()
Copy the code
  • ax
# Second way
fig,ax = plt.subplots()
ax.plot([1.2.3], [4.5.6])
plt.show()
Copy the code

The drawing effect is as follows

As you can see, the result is the same whether plt.plot() or ax.plot() is used

So what’s the difference?

From the code of the first way, the student becomes a Figure canvas and then implicitly generates a drawing area on this canvas to draw.

The second method generates both Figure and AXES objects, and then uses AX objects to plot in its area

From the perspective of object-oriented programming (important for understanding Matplotlib drawing), the second method is obviously easier to explain. The generated FIG and AX control the canvas Figure and drawing area Axes respectively, while the first method is not very intuitive. If it involves the setting of subgraph parts, Drawing in the first way is difficult.

The second method is also recommended for actual drawing.

Subplot drawing

This second plotting method will be further understood by introducing subplots

Now suppose I were to draw a line chart on the left and a scatter chart on the right of a piece of paper, how would I draw it?

First, there should be a canvas Figure. Secondly, there should be two Axes (equivalent to two subplots) to draw the graph

Generate canvas and AXES objects
# nrows=1 and nCOLs =2 represent 1 row and 2 columns, respectively
fig,ax = plt.subplots(nrows=1,ncols=2)
Copy the code

Since there are two drawing areas, ax corresponds to a list that stores two Axes objects.

Then control the left and right drawing areas respectively for drawing

fig,ax = plt.subplots(nrows=1,ncols=2)
ax[0].plot([1.2.3], [4.5.6])
ax[1].scatter([1.2.3], [4.5.6])
Copy the code

It turns out that one Axes object corresponds to a subplot, and these subplots are drawn on the same canvas Figure.

Now that you’ve read this, you’ve probably got a feel for Matplotlib plotting. The next article in the series will continue with the setup of common Matplotlib components.

Follow my official account “Python Reading Money” and reply “py” in the background to get the Python learning resources package