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

Review and

Plot module to draw line charts, bar charts, pie charts, etc., the content of the previous article is quickly viewed

  • Matplotlib Line chart Drawing: This section describes the line chart attributes

  • Matplotlib Bar chart drawing: This section describes the attributes of a bar chart

  • Matplotlib Histogram drawing: This section describes histogram attributes

  • Matplotlib Drawing a scatter diagram: This section describes the properties of the scatter diagram

  • Matplotlib Drawing a contour map: This section describes the properties of the contour map

  • Matplotlib Pie chart drawing: This section describes pie chart attributes

In statistical charts, data information can be displayed in detail. Often, we need to draw multiple subplots to help show data. Pyplo.subplot (), figure.add_Subplot, Pyplo.axes () and other methods were used to draw subplots of different scenes

In this issue, we will learn how matplotlib draws subgraphs, Let’s go~

1. Subgraph introduction

When we learned the bottom layer of Matplotlib module before, we all knew that matplotlib module was mainly divided into script layer, art layer and back end. In the art layer, we composed elements of graphs including Figure->Axes->Axis

  • How do I put another chart in a chart?

    At this point we need to add Axes objects to the Figure canvas to create the second chart.

  • The matplotlib module provides drawing methods

    • The matplotlib module provides methods
    methods instructions
    matplotlib.pyplot.subplot() Add Axes to the current canvas
    matplotlib.pyplot.subplots() Create a canvas and subgraph
    matplotlib.pyplot.subplot2grid() The use of a grid to evenly divide the different parts
    matplotlib.pyplot.axes() Adds the axis to the current canvas and makes it the current axis
    matplotlib.pyplot.figure() Create a new canvas
    matplotlib.Figure.add_subplot() Add a Axes object to the canvas as a subgraph
    matplotlib.Figure.subplots() Adds a set of subgraphs to the canvas
  • In the Matplotlib module, the methods supporting subplots are concentrated in the Pyplot and Figure classes

  • We usually use the main subplot(), subplots() and add_subplot() methods for the subplots provided by the matplotlib module

2. Subgraph attributes

The following properties will be shared between the subplot(), subplots(), and add_subplot() methods.

  • Sets the background color of the subimage

    • Keyword: Facecolor or FC
    • Value Optional:
      • Words for colors: e.g. Red, “red”
      • Short for color words such as red “r”, yellow “Y”
      • RGB format: hexadecimal format, such as “# 88C999 “; (r,g,b) tuple form
  • Sets the x/y label of the subgraph

    • Set the X-axis keyword to xlabel
    • Set the Y-axis keyword to ylabel
    • The value is a string
  • Set the x/y scale of the subgraph

    • Set the X-axis scale keyword: xscale
    • Set the Y-axis scale keyword: yscale
    • The value can be {“linear”, “log”, “symlog”, “logit”,… }
  • Set the aspect ratio of the subgraph:

    • Keyword: box_aspect
    • The value is a floating point
  • Set shaft position:

    • Keyword: set_position
    • The value is in list format, [left, bottom, width, height].

3. Draw subgraph steps

  1. Import the matplotlib.pyplot and matplotlib.Figure modules
  2. To prepare data, use numpy/ PANDAS
  3. Call the subgraph method to create a subgraph
  4. Call drawing graph methods line graph plot,pie,bar, etc

4. Test the cat

We use the Pyplot.subplot () method to plot subplots

  • First, we use Np.random.randint () to generate x and y axis data
import numpy as np
x = np.random.randint(0.100.25)

y = np.random.randint(0.100.25)
Copy the code
  • Create four subplot objects on the canvas by calling the pyplot.subplot() method with the for loop
import matplotlib.pyplot as plt
for i in range(1.5):
    plt.subplot(2.2,i,fc="#88c999",xlabel="x label",ylabel="y label",title="subplot",aspect="auto")
Copy the code
  • The pyplot.bar() method is called for each subgraph to draw a histogram
plt.bar(x,y)
Copy the code
  • Call the pyplot.tight_layout() method to adjust each subplot display screen
plt.tight_layout()
Copy the code
  • Finally, the subgraph is drawn as follows

conclusion

This time, we will learn the method of matplotlib subgraph drawing and related properties. The detailed operation of subgraph will continue to expand later.

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