Get the App
SLTechnology News&Howtos  ›  Development  › 

If Python implements graphic drawing,

Shulou Source: shulou.com Published: 2022-06-02 05:38:15 11月12日 Update

This article mainly introduces Python if the realization of graphics drawing, the article introduces in great detail, has a certain reference value, interested friends must read it!

1. Draw the first figure

The first figure starts with a simple one.

1.1Code # importing the required moduleimport matplotlib.pyplot as plt# x axis valuesx = [1jin2 importing the required moduleimport matplotlib.pyplot as plt# x axis valuesx 3] # corresponding y axis valuesy = [2pc4 L1] # plotting the pointsplt.plot (x, y) # naming the x axisplt.xlabel ('x-axis') # naming the y axisplt.ylabel ('y-axis') # giving a title to my graphplt.title ('My first graphics') # function to show the plotplt.show () 1.2output

1.3 partial interpretation of the code

1) define the x-axis and the corresponding y-axis values as lists.

2) use the .plot () function to draw them on the canvas.

3) use the .xlabel () and .ylabel () functions to name the x and y axes.

4) use the .title () function to name the drawing.

5) use the .show () function to view the drawing.

2. Draw two or more lines on the same graph

2. Draw two or more lines on the same graph

If you want to draw more lines on the same graph, you can use the .plot () function repeatedly.

2.1Code import matplotlib.pyplot as plt# line 1 pointsx1 = [1 plotting the line 2 pointsplt.plot 3] y1 = # plotting the line 1 pointsplt.plot (x1, y1, label = "line 1") # line 2 pointsx2 = [1 plotting the line 2 pointsplt.plot (x2, y2) Label = "line 2") # naming the x axisplt.xlabel ('x-axis') # naming the y axisplt.ylabel ('y-axis') # giving a title to my graphplt.title ('Two lines on same graphics') # show a legend on the plotplt.legend () # function to show the plotplt.show () 2.2 output

2.3 partial interpretation of the code

1) draw two lines on the same picture. They are distinguished by giving them a name (label), which is passed as an argument to the .plot () function.

2) A small rectangular box that provides information about the line type and its color is called a legend. You can use the .legend () function to add a legend to a drawing.

3. Custom drawing

Some basic customizations that apply to almost all scenarios are discussed below.

Code 3. 1 import matplotlib.pyplot as plt# x axis valuesx = [1, color='green', linestyle='dashed', linewidth 2, 3, 4, 5, 6] # corresponding y axis valuesy = [2, 4, 5, 5] # plotting the pointsplt.plot (x, y, color='green', linestyle='dashed', linewidth = 3, markerfacecolor='blue') Markersize=12) # setting x and y axis rangeplt.ylim (1JI 8) plt.xlim (1JI 8) # naming the x axisplt.xlabel ('x-axis') # naming the y axisplt.ylabel ('y-axis') # giving a title to my graphplt.title ('Some cool customizationsoutput') # function to show the plotplt.show () 3.2 output

3.3 partial interpretation of the code

As shown in the code above, we have made some custom changes:

1) set the width, style, and color of the line.

2) set the shape, color and size of the mark.

3) cover the x and y axis range. If the overlay is not complete, the pyplot module uses the automatic scaling feature to set the axis range and scale.

These are all the contents of the article "Python if you draw graphics". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Code function graphics drawing multiple partial color interpretation output content name legend article scope value information interest function parameter scene Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Linux Shulou Technology Shulou Information