How to realize graphic drawing with Python matplotlib
This article mainly explains "how to achieve graphics drawing with Python matplotlib". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to draw graphics with Python matplotlib".
1. Overview of matplotlib.patches
Matplotlib.patches is a class dedicated to drawing graphics, which is based on Artist
Pathes is a class that specializes in 2D graphics
Graphics drawn by patch are set to rc params by default
The patch module provides up to 10 graphical methods to meet daily needs
two。 Drawing method
Corresponding to the matplotlib module, the patches class provides methods for drawing circles, oval shapes, rectangles and so on.
3. Drawing steps
In the matplotlib module, the chart is composed of three basic elements: figure, Axes and Axis, so when drawing the graph, the general steps are mainly composed of the following.
Import matplotlib pyplot and patches classes
Import matplotlib.pyplot as pltimport matplotlib.patches as mpatch
Create a subgraph Axes object using subplots ()
Fig,ax = plt.subplots ()
Call the pathes class to draw graphics, such as drawing a rectangle Rectangle ()
Rect = mpatch.Rectangle ((0.2) 0.75), 0.4 (0.4) = "r")
Subgraph Axes object calls set_xlim () and set_ylim axis range
Patches by default, the coordinate range of the x-axis is (0prime1), and the coordinate range of the y-axis is (0recover1).
Ax.set_xlim (- 2) ax.set_ylim (- 2)
The subgraph Axes object calls the add_patch () method to add graphics.
Ax.add_patch (Rect)
Call pyplot.show () to display the graph
4. Draw graphics properties to set transparency
Keyword: alpha
The value type is: floating point
Set Color
Set graphic keywords: color
Set border keyword: edgecolor
Optional values are available:
English words for color: such as red "red"
Abbreviations for color words such as red "r" and yellow "y"
RGB format: hexadecimal format such as "# 88c999"; (rMagnegMagneb) tuple form
5. Try a bull's knife
After learning the above sections, let's draw circles, rectangles and straight lines in the chart.
Def drawpicture (): fig,ax = plt.subplots () Rect = mpatch.Rectangle ((1mem0.75), 0.4 Py 0.4 yellow = "yellow", alpha=0.5) Cri = mpatch.Circle ((0pc0), 1 dagger 30 Py = "pink", alpha=0.2,cap) Py = mpatch.Arrow (1mem2) 2) ax.set_xlim (- 1) ax.set_ylim (- 1) 5) ax.add_patch (Rect) ax.add_patch (Cri) ax.add_patch (Py) plt.show () drawpicture ()
At this point, I believe that you have a deeper understanding of "Python matplotlib how to achieve graphics drawing", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!