代码实例
# coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
# x, y为两个数列,对应的x[k],y[k]便是二位平面上的一个点
# x是从0.0开始到2.0结束(不包括),以0.01为步长的数列
x = np.arange(0.0, 2.0, 0.01)
y = np.sin(2 * np.pi * x) # 同样是一个数列,x的sin函数,注意nunpy中默认使用弧度
fig, ax = plt.subplots() # 创建一个绘图界面
ax.plot(x, y) # 在界面上使用plot方法绘制曲线
ax.plot(x, y, '--') # 在界面上使用plot方法绘制短线
ax.plot(x, y, 'c*') # 在界面上使用plot方法绘制青色星号
ax.plot(x, y, 'kx') # 在界面上使用plot方法绘制黑色叉号
ax.plot(x, y, 'ro') # 在界面上使用plot方法绘制红色圆圈
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='sin(x)') # 设置x轴和y轴的标签,曲线的名称
ax.grid() # 显示网格
plt.show() # 显示图片
运行结果
ax.plot(x, y)