Machine learning-python basics two, Matplotlib tutorial

Two, Matplotlib tutorial

1 Introduction

Matplotlib is probably the most widely used package in the field of Python 2D-plotting. It allows users to easily graph data and provide a variety of output formats. Here will explore the common usage of matplotlib.

pylab is an interface of matplotlib's object-oriented drawing library. Its syntax is very similar to Matlab. In other words, its main drawing commands and Matlab corresponding commands have similar parameters.

2. Use in python

For mac osX systems, you must add the second line, otherwise an error will be reported!
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
The python version must be set to 3.0

https://blog.csdn.net/joey_ro/article/details/109101392

Chinese fonts must be introduced or an error will be reported

Matplotlib does not support Chinese by default, we can use the following simple method to solve it.

Here we use Siyuan Heidi, which is an open source font launched by Adobe and Google.

Official website: https://source.typekit.com/source-han-serif/cn/

GitHub address: https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese

You can also download it from the network disk: https://pan.baidu.com/s/14cRhgYvvYotVIFkRVd71fQ Extraction code: e15r .

You can download an OTF font, such as SourceHanSansSC-Bold.otf, and place the file in the currently executing code file:

The SourceHanSansSC-Bold.otf file is placed in the currently executing code file:

# fname 为 你下载的字体库路径,注意 SourceHanSansSC-Bold.otf 字体的路径
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf")

3. Some important functions

1) Subplot displays multiple functions on a graph
# 计算正弦和余弦曲线上的点的 x 和 y 坐标
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# 建立 subplot 网格,高为 2,宽为 1
# 激活第一个 subplot
plt.subplot(2, 1, 1)
# 绘制第一个图像
plt.plot(x, y_sin)
plt.title('正弦函数', fontproperties=zhfont1)
# 将第二个 subplot 激活,并绘制第二个图像
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('余弦函数', fontproperties=zhfont1)
# 展示图像
plt.show()

2) Bar graph
# bar函数生成条形图
x = [5, 8, 10]
y = [12, 16, 6]
x2 = [6, 9, 11]
y2 = [6, 15, 7]
plt.bar(x, y, align='center')
plt.bar(x2, y2, color='g', align='center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
3) Frequency distribution diagram

bin is the limit

data = np.array(
    [1, 233, 45, 545, 543, 5, 34534, 5, 345, 34, 5, 34, 53, 45, 34, 5, 34, 5, 345, 3, 45, 34, 5, 34, 534, 5, 34])
plt.hist(data, bins=[0, 20, 40, 100])
plt.show()

Guess you like

Origin blog.csdn.net/joey_ro/article/details/109102230