Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

If Python implements graphic drawing,

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report