在使用Matplotlib进行可视化时,最简单的就是线性方程 y = f (x) 的可视化了。来看看如何创建这个简单的线形图。首先需要导入以下库:
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn-whitegrid')
要画 Matplotlib 图形时,都需要先创建一个图形 fig 和一个坐标轴 ax。创建图形与坐标轴的最简单做法如下所示:
fig = plt.figure()
ax = plt.axes()
创建好坐标轴之后,就可以用 ax.plot 画图了。从一组简单的正弦曲线(sinusoid)开始:
fig = plt.figure()
ax = plt.axes()
x = np.linspace(0, 10, 1000)
ax.plot(x, np.sin(x))