Summary of methods for drawing line charts in matplotlib in Python

Summary of the method of drawing a line graph in matplotlib in Python (it is enough to read this blog)

    This article mainly records how to draw a line chart with the built-in library matplotlib in Python.

1. Import library

    Import the matplotlib library directly.

import matplotlib.pyplot as plt

2. Data preparation

    Use list to prepare data for the abscissa and ordinate respectively.

X1 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y1 = [971,1344,1953,3064,4766,15522,4389,2615,1696,1210,798]
X2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y2 = [554,861,1238,1979,3206,10663,2916,1639,1047,704,489]
X3 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y3 = [363,547,801,1254,2205,7535,1984,1115,677,464,340]
X4 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y4 = [293,478,681,1070,1819,6563,1750,953,583,410,287]

3. Image drawing

    Draw a line chart directly.

plt.figure()

# X1的分布
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

# X2的分布
plt.plot(X2, Y2,label="X2",  color="#3399FF", marker='o', linestyle="-")

# X3的分布
plt.plot(X3, Y3, label="X3",  color="#F9A602", marker='s', linestyle="-")

# X4的分布
plt.plot(X4, Y4, label="X4",  color="#13C4A3", marker='d', linestyle="-")
# plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来
X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
plt.xticks(X1,X_labels,rotation=0)
plt.legend()
plt.title("Lake.bmp")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

    The above results are pro-test valid.

4. Complete code (directly copy and run)

import matplotlib.pyplot as plt
X1 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y1 = [971,1344,1953,3064,4766,15522,4389,2615,1696,1210,798]
X2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y2 = [554,861,1238,1979,3206,10663,2916,1639,1047,704,489]
X3 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y3 = [363,547,801,1254,2205,7535,1984,1115,677,464,340]
X4 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y4 = [293,478,681,1070,1819,6563,1750,953,583,410,287]

plt.figure()

# X1的分布
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

# X2的分布
plt.plot(X2, Y2,label="X2",  color="#3399FF", marker='o', linestyle="-")

# X3的分布
plt.plot(X3, Y3, label="X3",  color="#F9A602", marker='s', linestyle="-")

# X4的分布
plt.plot(X4, Y4, label="X4",  color="#13C4A3", marker='d', linestyle="-")
# plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来
X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
plt.xticks(X1,X_labels,rotation=0)
plt.legend()
plt.title("Lake.bmp")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

    The drawing effect is shown in the figure below:

insert image description here

5. How to set the line type, line color, dot mark on the line, and X-axis labels

5.1 How to set the line type

The line type setting is mainly set through plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")the parameter in     the statement:, the parameter value and its meaning type are compared as follows:linestyle=
insert image description here

5.2 How to set the line color

The line color setting is mainly set through plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")the parameters in     the statement:, the parameter value and its meaning type are compared as follows:     In addition, the color can be set through the RGB value of the color, for example, the statement: is to set the RGB value of the color: #FF3B1D, Record the RGB values ​​of several commonly used colors, as follows:color=
insert image description here
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

serial number RGB value color effect
1 #FF3B1D red
2 #3399FF blue
3 #F9A602 yellow
4 #13C4A3 green
5 #FF652D orange
6 #D09E88 Earthy
7 #CC7112 dark brown
8 #F2BDD0 Pink

For more bright colors, please refer to: Bright Color RGB Value Website Link

5.3 How to set the online dot mark

plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")The line type setting is mainly set through the parameters in     the statement: marker=If you do not need to mark the data points with dots, you do not need to set this parameter, just delete the parameter setting directly. The comparison between the parameter value and its meaning type is as follows :
insert image description here

5.4 How to set the X-axis scale labels

    When the scale of the X-axis is not artificially set, the X-axis of the drawn image will skip certain values. For example, when the range of the X-axis is [-5,5], only -4, -2, 0, 2, 4 these points, and the value in the middle is directly omitted. If you want to display these values, there are three solutions: (1)
    directly convert the scale value of X into a string, that is to say, directly convert Each element in the X1, X2, X3, and X4 collections can be directly converted into a string type. Add the following line of code after the assignment statement for x1:

X1 = [str(i) for i in X1]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X2 = [str(i) for i in X2]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X3 = [str(i) for i in X3]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X4 = [str(i) for i in X4]    #为了让每个值不被省略,把list中所有的元素都转化成str格式

    The complete code is as follows:

import matplotlib.pyplot as plt
X1 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y1 = [971,1344,1953,3064,4766,15522,4389,2615,1696,1210,798]
X2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y2 = [554,861,1238,1979,3206,10663,2916,1639,1047,704,489]
X3 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y3 = [363,547,801,1254,2205,7535,1984,1115,677,464,340]
X4 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y4 = [293,478,681,1070,1819,6563,1750,953,583,410,287]

X1 = [str(i) for i in X1]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X2 = [str(i) for i in X2]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X3 = [str(i) for i in X3]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
X4 = [str(i) for i in X4]    #为了让每个值不被省略,把list中所有的元素都转化成str格式

plt.figure()

# X1的分布
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

# X2的分布
plt.plot(X2, Y2,label="X2",  color="#3399FF", marker='o', linestyle="-")

# X3的分布
plt.plot(X3, Y3, label="X3",  color="#F9A602", marker='s', linestyle="-")

# X4的分布
plt.plot(X4, Y4, label="X4",  color="#13C4A3", marker='d', linestyle="-")
# plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来
# X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
# plt.xticks(X1,X_labels,rotation=0)
plt.legend()
plt.title("Lake.bmp")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

    (2) Set the display interval of the X-axis data directly with the code, which MultipleLocator(1)means that the interval between the X-axis display data is 1.

import matplotlib.ticker as ticker
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来

    The complete code is as follows:

import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
X1 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y1 = [971,1344,1953,3064,4766,15522,4389,2615,1696,1210,798]
X2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y2 = [554,861,1238,1979,3206,10663,2916,1639,1047,704,489]
X3 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y3 = [363,547,801,1254,2205,7535,1984,1115,677,464,340]
X4 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y4 = [293,478,681,1070,1819,6563,1750,953,583,410,287]

plt.figure()

# X1的分布
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

# X2的分布
plt.plot(X2, Y2,label="X2",  color="#3399FF", marker='o', linestyle="-")

# X3的分布
plt.plot(X3, Y3, label="X3",  color="#F9A602", marker='s', linestyle="-")

# X4的分布
plt.plot(X4, Y4, label="X4",  color="#13C4A3", marker='d', linestyle="-")
plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来,X轴数据显示间隔为1
plt.legend()
plt.title("Lake.bmp")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

    (3) By directly resetting the label list X_labels for the X-axis, the display of the X-axis can be modified, not only can be changed to a string of values, but also can be changed to other string forms. rotation=0Indicates that the display of the X-axis label is rotated by 0 degrees. The rotation is to prevent some X-axis labels from being too long, and the display will overlap. Therefore, it is rotated to free up more display space.

import matplotlib.ticker as ticker
X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
plt.xticks(X1,X_labels,rotation=0)

    The display effect is shown in the figure below:
insert image description here

    After rewriting rotation=0 to rotation=20, the X-axis label display effect changes as follows:
    the code is:

X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
plt.xticks(X1,X_labels,rotation=20)

    The display effect is:
insert image description here
    You can also rewrite the X coordinate to the desired label form according to your needs, for example, change it to a letter:

X_labels = ['-a','-b','-c','-d','-e','a','b','c','d','e','f']
plt.xticks(X1,X_labels,rotation=20)

    The display effect is:
insert image description here

    The full code is:

import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
X1 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y1 = [971,1344,1953,3064,4766,15522,4389,2615,1696,1210,798]
X2 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y2 = [554,861,1238,1979,3206,10663,2916,1639,1047,704,489]
X3 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y3 = [363,547,801,1254,2205,7535,1984,1115,677,464,340]
X4 = [-5,-4,-3,-2,-1,0,1,2,3,4,5]
Y4 = [293,478,681,1070,1819,6563,1750,953,583,410,287]

# X1 = [str(i) for i in X1]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
# X2 = [str(i) for i in X2]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
# X3 = [str(i) for i in X3]    #为了让每个值不被省略,把list中所有的元素都转化成str格式
# X4 = [str(i) for i in X4]    #为了让每个值不被省略,把list中所有的元素都转化成str格式

plt.figure()

# X1的分布
plt.plot(X1, Y1, label="X1", color="#FF3B1D", marker='*', linestyle="-")

# X2的分布
plt.plot(X2, Y2,label="X2",  color="#3399FF", marker='o', linestyle="-")

# X3的分布
plt.plot(X3, Y3, label="X3",  color="#F9A602", marker='s', linestyle="-")

# X4的分布
plt.plot(X4, Y4, label="X4",  color="#13C4A3", marker='d', linestyle="-")
# plt.gca().xaxis.set_major_locator(ticker.MultipleLocator(1))   # 将横坐标的值全部显示出来
X_labels = ['-5','-4','-3','-2','-1','0','1','2','3','4','5']
plt.xticks(X1,X_labels,rotation=20)
plt.legend()
plt.title("Lake.bmp")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

6. Reference link

    There are mainly the following four reference links:
    (1) Python xticks() function to set the X-axis method - scale, label
    (2) python drawing (plt.) What should I do if the x-axis abscissa is omitted (interrupted) | How to make all The x values ​​of the abscissa are displayed | How to adjust the angle of the abscissa
    (3) Detailed explanation of matplotlib.pyplot.plot() parameters
    (4) Python data analysis: drawing line charts and scatter plots

Guess you like

Origin blog.csdn.net/weixin_43981621/article/details/123095683