設定は通常、plt.*
matplotlib の現在のプロットに適用されます。を使用するとplt.subplot
、新しいプロットが開始されるため、設定は適用されなくなります。Axes
プロットに関連付けられたオブジェクトを通過することで、ラベル、目盛りなどを共有できます (こちらの例を参照)。代わりに、共通の「スタイリング」を 1 つの関数に入れ、それをプロットごとに呼び出すことを提案します。
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
同様に、が 2 番目のプロットの下にのみ表示されるだけで十分な場合があります。
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()