python in the Matplotlib drawing straight line example code

  • 2021-11-14 06:01:49
  • OfStack

Description

1. Import the module pyplot and specify the alias plt to avoid typing pyplot repeatedly. Modular pyplot contains many functions for making charts.

2. Pass the coordinates of the drawn line to the function plot ().

3. Open Matplotlib through the function plt. show () to display the drawing.

Instances


import matplotlib.pyplot as plt
# Will (0,1) Point sum (2,4) Connect 
plt.plot([0,2],[1,4])
plt.show()

Related instance extensions:

Linear diagram


import matplotlib.pyplot as plt
import numpy as np

#  Specify the seed for generating random numbers, so that the random numbers obtained by each run are the same 
np.random.seed(42)
#  Generate 30 Satisfy the average value of 0 The variance is 1 Samples of normal distribution of 
x = np.random.randn(30)
# plot The original meaning is "drawing ( Chart ) The meaning of ", so the following here 1 The line of code is to draw a diagram. r Specifies that the color of the drawn line is red, o Specifies that the shape used to mark the actual point is a circle, -- Specifies that the line shape is a dotted line 
plt.plot(x, "r--o")
#  Show chart 
plt.show()

Line color, mark shape and line type

Common parameters of line color:

"b": Specifies that the line color to be drawn is blue.
"g": Specifies that the line color to be drawn is green.
"r": Specifies that the line color to be drawn is red.
"c": Specifies that the line color to be drawn is blue-green.
"m": Specifies that the line color to be drawn is magenta.
"y": Specifies that the line color to be drawn is yellow.
"k": Specifies that the line color to be drawn is black.
"w": Specifies that the line color to be drawn is white.

Common parameters of marker shape:

"o": Specifies that the shape used to mark the actual point is a circle.
"*": Specifies that the shape used to mark the actual point is a star.
"+": Specifies that the shape used to mark the actual point is a plus sign shape.
"x": Specifies that the shape used for marking the actual point is an x shape.

Common parameters of line type:

"-": Specifies that the line shape is a solid line.
"": Specifies that the line shape is a dotted line.
"-.": Specifies that the line shape is a dotted solid line.
":": Specifies that the line shape is a dotted line.


Related articles: