一些主要涉及图形样式的设置可以使用全局设置matplotlib rc 参数。
例如,在整个脚本中设置网格,将
plt.rcParams['axes.grid'] = True
在文件的开头(导入之后)。
其他诸如轴限制之类的东西实际上是特定于绘图本身的,并且没有全局参数。但您仍然可以按照链接问题中所述的方式进行操作,即编写自己的函数来完成您需要的大部分操作。
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['axes.grid'] = True
def plotfunction(fig, x1, y1, x2, y2,
title1 = 'Surface concentrations',
title2 = 'Surface temperature', **kwargs ):
ax = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex=ax)
ax.set_title(title1)
ax2.set_title(title2)
ax.set_ylim(1e-3, None)
ax2.set_ylim(1e-3, None)
ax.plot(x1, y1, **kwargs)
ax2.plot(x2, y2, **kwargs)
fspec = plt.figure(1)
fener = plt.figure(2)
x1, y1, x2, y2 = np.loadtxt("spectrum.txt", unpack=True)
plotfunction(fspec,x1, y1, x2, y2)
x1, y1, x2, y2 = np.loadtxt("energy.txt", unpack=True)
plotfunction(fener,x1, y1, x2, y2, linewidth=3)