Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python library matplotlib to draw different charts

Shulou Source: shulou.com Published: 2022-06-01 03:12:50 09月10日 Update

This article mainly introduces how to use python library matplotlib to draw different charts, the article is very detailed, has a certain reference value, interested friends must read it!

1. Draw a simple graph

Idea: draw a curve through three coordinate points

Import matplotlib.pyplot as plt plt.plot ([1JEI 3JEI 5], [4JEI 8JEI 10]) # Abscissa: 1pje 3pr 5, ordinate: 4je 8je 1Ji displays the drawing plt.show ()

The running effect is as shown in the figure:

2. Draw a single graph

Idea: first draw a straight line through linspace, then define 100 elements between-pi~pi

Import matplotlib.pyplot as pltimport numpy as np x = np.linspace (- np.pi, np.pi, 100) # x axis is defined from-pi to pi, and then define 100elements plt.plot (x, np.sin (x)) # between-pi~pi to show the drawing plt.show ()

The running effect is as shown in the figure:

3. Draw multiple curves

Idea: on the basis of drawing a curve, modify the definition field, and then loop through plt.plot ()

Import matplotlib.pyplot as pltimport numpy as np x = np.linspace (- np.pi * 2, np.pi * 2100) # the definition domain of the x axis is-2pi~2piplt.figure (1, dpi=50) # create Chart 1 with an accuracy of 50. The higher the precision, the larger the volume of the picture. The clearer the picture is, the clearer for i in range (1,5): # such as drawing four curves plt.plot (x, np.sin (x / I)) # shows the drawing plt.show ()

The running effect is as shown in the figure:

4. Draw histogram

Idea: use the hist () method

Import matplotlib.pyplot as plt plt.figure (1) dpi = 50) # create chart 1 import matplotlib.pyplot as plt plt.figure DPI represents the fineness of the picture. The larger the dpi, the larger the file. Data = [1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 4] plt.hist (data) # as long as the data is passed in, the histogram will count the number of times the histogram appears # display the drawing plt.show ()

The running effect is as shown in the figure:

5. Draw scatter plot

Idea: use the scatter () method

Import matplotlib.pyplot as pltimport numpy as np x = np.arange (1,10) y = xfig = plt.figure () # create a chart plt.scatter (x, y, cased scatterers, marker='o') # cased scattersThe color of the scatter is red, and marker indicates that the shape of the scatter is specified as a circle. # display the drawing plt.show ()

The running effect is as shown in the figure:

The above is all the contents of the article "how to draw different diagrams using python library matplotlib". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Curves charts ideas effects such as diagrams runs pictures definition fields differences between elements content data methods graphs histograms articles precision representation value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Docker Linux MariaDB