import matplotlib.pyplot as plt
import numpy as np
# Data for plotting
t = np.arange(0.0,2.0,0.01)
s =1+ np.sin(2* np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s, label='sin(x)')
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks')
ax.grid()# 栅格
ax.legend()# 图例
fig.savefig("test.svg",format='svg')# 以svg格式保存
多个子图
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,5,20)
fig, axs = plt.subplots(2,2)
axs[0][0].plot(x, x, label='x')
axs[0][1].plot(x, x **2, label='x^2')
axs[1][0].plot(x, x **3, label='x^3')
axs[1][1].plot(x, x **4, label='x^4')for ax in axs.flat:
ax.legend()