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

preface

We have studied the underlying structure of the Matplotlib module before. The previous article lists the methods of drawing statistical charts such as line chart, bar chart, pie chart and histogram provided by its Pyplot class (script layer) as follows.

  • The matplotlib module is divided into script layer, art layer and back-end, which helps to learn workflow

  • Matplotlib line plot: Summarize the properties of pyplot.plot() line plot

  • Matplotlib histogram plotting: Summarize the properties of the histogram plotted by Pyplot.bar ()

  • Histogram drawn by matplotlib: Pyolot.hist () draws histogram properties for summary description

Matplotlib module not only provides the function of drawing statistical charts, but also supports drawing circles, squares, rectangles and other graphs

In this installment, we will learn how to use the Matplotlib module to draw common graphics. Let’s go~

1. Matplotlib. Summary of patches

Matplotlib. patches A class dedicated to drawing graphics, based on Artist

  • Pathes is a class dedicated to drawing 2D graphics
  • The default setting for patch graphics is RC Params
  • The Patch module provides up to 10 graphical methods to meet your daily needs

2. Drawing methods

For the Matplotlib module, the Patches class provides methods for drawing shapes such as circles, ellipses, and rectangles

methods role
patches.Rectangle(xy,width,height,angle=0) Draw a rectangle
patches.Polygon(xy) Draw polygons
patches.Arc(xy,width,height,angle=0) Draw an ellipse
patches.Circle(xy,radius) Draw a circular
Patches. The Ellipse (x, y, width, height, Angle = 0.0) Draw an ellipse
patches.Arrow(x,y,dx,dy) Draw a haircut
patches.wedge(center,r,theta1,theta2,width) Draw this shape
patches.PathPatch() Draw multi-curve graphics
patches.FancyBboxPatch() Draw fancy graphics box
patches.Line2D() Draw lines

3. Drawing steps

In the matplotlib module, charts are composed of three basic elements, figure, Axes and Axis, so when drawing graphs, general steps are mainly composed of the following.

  • Import the Matplotlib Pyplot and Patches classes
import matplotlib.pyplot as plt
import matplotlib.patches as mpatch
Copy the code
  • Use the subplots() to create child Axes objects
fig,ax =plt.subplots()
Copy the code
  • Call the Pathes class to draw a Rectangle, for example, Rectangle().
Rect = mpatch.Rectangle((0.2.0.75),0.4.0.4,color="r")
Copy the code
  • The subaxes object calls the set_xlim() and set_ylim Axes ranges

Patches by default, the X-axis coordinate range is (0,1), and the Y-axis coordinate range is (0,1).

ax.set_xlim(-2.5)
ax.set_ylim(-2.5)
Copy the code
  • The add_patch() method is called by the subaxes object to add the graph
ax.add_patch(Rect)
Copy the code
  • Call Pyplot.show () to show the graph

4. Draw graph attributes

  • Set transparency

    • Key words: alpha
    • The value can be floating point
  • Set the color

    • Set the graphic keyword: color
    • Set the border keyword: edgecolor
    • 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

5. Test the cat

After studying the previous sections, let’s draw circles, rectangles and lines on the diagram

def drawpicture() :

    fig,ax =plt.subplots()

    Rect = mpatch.Rectangle((1.0.75),0.4.0.4,color="yellow",alpha=0.5)

    Cri = mpatch.Circle((0.0),1,angle=30,color="pink",alpha=0.2,capstyle="round")

    Py = mpatch.Arrow(1.2.2.2)

    ax.set_xlim(-1.5)
    ax.set_ylim(-1.5)

    ax.add_patch(Rect)
    ax.add_patch(Cri)
    ax.add_patch(Py)
    plt.show()

drawpicture()
Copy the code

conclusion

In this installment, we will learn the methods and steps related to matplotlib graphing. In practice, we need a lot of practice to become more proficient in using it

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