Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use matplotlib to realize Visual drawing in Python

Shulou Source: shulou.com Published: 2022-06-03 08:42:09 10月04日 Update

Editor to share with you how to use matplotlib to achieve visual mapping in Python. I hope you will get something after reading this article. Let's discuss it together.

First, draw linear graphics

Execute the following code

Import matplotlib.pyplot as pltdataX= [1 dataX,dataY 2 3 4] dataY= [2 dataX,dataY 4 4 4] plt.plot (dataX,dataY) plt.title ("Draw straight line") plt.xlabel ("x") plt.ylabel ("y") plt.show ()

The above statement draws a straight line whose shape is determined by the x and y coordinate values. Run the program to get the following results

Second, draw columnar figures

Execute the following code

Import matplotlib.pyplot as pltdataX= [0Jing 1, 2, 3, 4, 5] dataY= [1, dataX,dataY, 2, 3, 2, 4, 4] plt.bar (dataX,dataY) plt.title ("Draw Histogram") plt.xlabel ("x") plt.ylabel ("y") plt.show ()

Six bar charts are drawn by the above statement and implemented with the function plt.bar. The following results are obtained by running the program

Bar charts can also be drawn with numpy, executing the following code

Import matplotlib.pyplot as pltimport numpy as npx=np.arange (10) y=np.random.randint (0Jing 20pm 10) plt.bar (XJI y) plt.show ()

Use the function random () to draw the bar chart that randomly appears in the area. In y=np.random.randint (0Ling 20 ~ 10), parameter 20 represents the height of the bar chart, and 10 represents the number of column charts. The running result is as follows

Third, draw histogram

Execute the following code

Import matplotlib.pyplot as pltimport numpy as npmean,sigma=0,1x=mean + sigma * np.random.randn (10000) plt.hist (XM50) plt.show ()

The above statement draws the probability distribution histogram, and the parameter mean=0 represents that the mean value is 0 and sigma deviation 1 means that the standard deviation is 1. Run the program to get the following results

Fourth, draw a scatter plot

Execute the following code

Import matplotlib.pyplot as pltimport numpy as npx=np.random.rand (100,100) y=np.random.rand (100,100) plt.scatter (xpene y) plt.show ()

The above statement draws a scatter chart. Np.random.rand (100) represents a random number of points with a total of 100 points. Run the program to get the following results

Fifth, draw polar coordinates

Execute the following code

Import matplotlib.pyplot as pltimport numpy as nptheta=np.arange (0jingnp.pijing0.02) ax1=plt.subplot (121pcmcmpm.pjpm.polarn) ax1.plot (theta,theta/6,'--',lw=2) plt.show ()

The above statement draws the polar coordinate diagram, which is often used in the comparison of visual data models of enterprises. It is realized by using polar function, and theta represents the plane angle in mathematics. The following results can be obtained by running the program.

Draw a pie chart

Execute the following code

Import matplotlib.pyplot as pltimport numpy as npplt.title ("Pie") labels='Math','Computer','Music','Art'sizes= [45 counterclock=Falseplt.pie (sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90) plt.show ()

The above statement draws the pie chart and runs the program to get the following results

If you want to highlight a part, you can change 0 to 1 in the statement explode=, for example, explode= (0min0.1) to get the following picture

After reading this article, I believe you have a certain understanding of "how to use matplotlib to achieve visual drawing in Python". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Code result statement run program column representative visualization function drawing parameter graph polar coordinate histogram article following figure number enterprise region mean Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS NVidia Shulou Information Microsoft