plt.*
设置通常适用于 matplotlib 的 当前 绘图;使用 plt.subplot
,您将开始一个新的绘图,因此设置不再适用于它。您可以通过与绘图相关的 Axes
对象(在此处查看示例)共享标签、刻度等,但在我看来这可能会过度使用。相反,我建议将共同的“样式”放入一个函数中,并对每个绘图调用该函数:
def applyPlotStyle():
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')
plt.xticks(range(100), rotation=30, size='small')
plt.grid(True)
plt.subplot(211)
applyPlotStyle()
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
plt.subplot(212)
applyPlotStyle()
plt.yscale('log')
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
顺便提一句,你可以通过将绘图命令提取到这样一个函数中来消除更多的重复:
def applyPlotStyle():
plt.xlabel('Size')
plt.ylabel('Time(s)');
plt.title('Matrix multiplication')
plt.xticks(range(100), rotation=30, size='small')
plt.grid(True)
def plotSeries():
applyPlotStyle()
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
plt.subplot(211)
plotSeries()
plt.subplot(212)
plt.yscale('log')
plotSeries()
另外一方面,将标题放在图的顶部(而不是在每个图上方)可能已经足够了,例如使用
suptitle
。同样,
xlabel
只需出现在第二个图下方即可。
def applyPlotStyle():
plt.ylabel('Time(s)');
plt.xticks(range(100), rotation=30, size='small')
plt.grid(True)
def plotSeries():
applyPlotStyle()
plt.plot(xl, serial_full, 'r--')
plt.plot(xl, acc, 'bs')
plt.plot(xl, cublas, 'g^')
plt.suptitle('Matrix multiplication')
plt.subplot(211)
plotSeries()
plt.subplot(212)
plt.yscale('log')
plt.xlabel('Size')
plotSeries()
plt.show()