补充:matplotlib

机器学习2(补充:Matplotlib)

matplotlib及其子包PyPlot时实现可视化二维数据的基本工具,PyPlot的核心时plot方法,实现为传入一个列表或者一维数组,只要传入一个参数,就会假设该值为y值序列,并自动生成x值。可以接受指明线条属性的参数。

示例代码:

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

x=np.arange(0.,5.,0.2)
plt.plot(x,x**4,'r',x,x*90,'bs',x,x**3,'g^')
plt.show()
print(help(plt.plot))

图形显示结果如下:

程序运行结果

也可以使用help(plt.plot)获得完整的线条风格列表,截取一部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
Parameters
----------
x, y : array-like or scalar
The horizontal / vertical coordinates of the data points.
*x* values are optional. If not given, they default to
``[0, ..., N-1]``.

Commonly, these parameters are arrays of length N. However,
scalars are supported as well (equivalent to an array with
constant value).

The parameters can also be 2-dimensional. Then, the columns
represent separate data sets.

创建多个坐标轴,使用subplot命令