绘制图片
import numpy as np
import matplotlib.pyplot as plt
# 双变量的直方图
# 颜色越深频率越高
# 研究双变量的联合分布
x = np.arange(2).reshape(-1,1)
y = np.arange(2,4).reshape(-1,1)
z = np.hstack([x, y]) # 横向连接两个矩阵
ax1 = plt.imshow(z)
plt.colorbar()
plt.xlabel('activities')
plt.ylabel('sensor')
plt.title('significance analysis')
plt.show()
绘制子图
# import matplotlib.pyplot as plt
import numpy as np
def f(t):
s1 = np.cos(2*np.pi*t)
e1 = np.exp(-t)
return s1 * e1
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
t3 = np.arange(0.0, 2.0, 0.01)
fig, axs = plt.subplots(2, 2, constrained_layout=True)
axs[0][1].plot(t1, f(t1), 'o', t2, f(t2), '-')
axs[0][1].set_title('subplot 1')
axs[0][1].set_xlabel('distance (m)')
axs[0][1].set_ylabel('Damped oscillation')
fig.suptitle('This is a somewhat long figure title', fontsize=16)
axs[1][0].plot(t3, np.cos(2*np.pi*t3), '--')
axs[1][0].set_xlabel('time (s)')
axs[1][0].set_title('subplot 2')
axs[1][0].set_ylabel('Undamped')
plt.show()
防止 plt
out of memory
from matplotlib import pyplot
while True:
fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
ax.legend(legendStrings, loc = 'best')
fig.savefig('himom.png')
plt.clf()
# etc....
ref
Customizing Matplotlib with style sheets and rcParams
Controlling the position and size of colorbars with Inset Axes
matplotlib.pyplot.subplots_adjust
Only Using Plt.Close. (2021) python - Matplotlib runs out of memory when plotting in a loop - Stack Overflow. Retrieved April 06, 2021, from https://stackoverflow.com/questions/2364945/matplotlib-runs-out-of-memory-when-plotting-in-a-loop