SoFunction
Updated on 2024-11-18

python matplotlib various drawings

1. Introduce the matpltlib library

matplotlibIt is an excellentpythonThird-party libraries for data visualization
utilizationmatpltlibWhen the library draws, bring it in first, load the pyplot inside and name it plt, then use theplotGraphing functions

import  as plt #plt is an alias for the introduced module


Summary of Basic Iconic Functions

Function Graphing Syntax Rules

The plot function argument:plot([x],y,[format],**kwargs)

There are too many different kinds of grammar, so I'll steal a few MOOC charts and put them up.

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4. Line graphs

from matplotlib import pyplot as plt

# Generate data
# Horizontal coordinate data from 2017 to 2022, the third parameter can control the step size, can write or not write
x = range(2017, 2022)
The value of the vertical coordinate of #y
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
# Generate graphics
("LMY and her mother's weight")
('year')
('kg')
(x, y1, color='green', label='LMY')
(x, y2, color='purple', label='mother')
(alpha=0.5)
(loc='upper right')
#Display Graphics
()

请添加图片描述

4. Scatterplot

from matplotlib import pyplot as plt
import numpy as np

# Generate data
# Horizontal data from 2017 to 2022, the third parameter controls the step size and can be written or not.
x = range(2017, 2022)
# y corresponds to the value of the vertical coordinate
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
# Generate graphics
("LMY and her mother's weight")
('year')
('kg')
# The size of the point
area = *4**2
(x, y1, s=area, c='yellow', alpha=1)
(x, y2, s=area, c='blue', alpha=1)
()
(())
()

请添加图片描述

5. Histogram

from matplotlib import pyplot as plt
import numpy as np

# Generate data
# Horizontal data from 2017 to 2022, the third parameter controls the step size and can be written or not.
x = [2017, 2018, 2019, 2020, 2021]
# y corresponds to the value of the vertical coordinate
y1 = [49, 48, 45, 52, 50]
y2 = [60, 62, 61, 65, 63]
# Generate graphics
("LMY and her mother's weight")
('frequency')
('kg')
# The size of the point
(y1, bottom=None, color='purple')
(y2, bottom=None, color='pink')
()

# n, bins, patches = (arr, bins=50, normed=1, facecolor='green', alpha=0.75)
'''
arr:a one-dimensional array to calculate the histogram.
bins:the number of bars in the histogram, optional, default is 10
normalized:whether to normalize the histogram vector, default is 0.
facecolor:color of the histogram.
alpha:transparency
'''

在这里插入图片描述

6. Bar charts

vertically

from matplotlib import pyplot as plt
import numpy as np

arr = (2017, 2022)
x = [49, 48, 45, 52, 50]  # x-axis
y = [2017, 2018, 2019, 2020, 2021]
rect = (arr, x, width=0.5)
('LMY')
('weight')
('year')
()

()

请添加图片描述

orthogonal

请添加图片描述

many

from matplotlib import pyplot as plt
import numpy as np

arr = (2017, 2022)
x1 = [49, 48, 45, 52, 50]  # x-axis
x2 = [60, 62, 61, 65, 63]
y = [2017, 2018, 2019, 2020, 2021]
rects1 = (arr, x1, 0.5, color='purple', label='LMY')
rects2 = (arr, x2, 0.5, color='yellow', label='Mother', alpha=0.3)
("LMY and her mother's weight")
('weight')
('year')
()

()

请添加图片描述

7. Pie charts

from matplotlib import patches, pyplot as plt
import numpy as np

label_list = ['49', '48', '45', '52', '50']
size = [20, 20, 20, 20, 20]
# The color of the parts
color = ['red', 'pink', 'blue', 'green', 'purple']
explode = [0, 0, 0.15, 0, 0]

patches, l_text, p_text = (size, explode=explode, colors=color, labels=label_list,
                                  labeldistance=1.2, autopct="%1.2f%%", shadow=False, startangle=90, pctdistance=0.6)
('equal')
("LMY's weight")
(loc='upper left')
()

请添加图片描述

to this article on python matplotlib various drawing of the article is introduced to this, more related python matplotlib drawing content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!