私は2Dプロットをカスタマイズしました。今後のプロットで軸、ラベル、フォントサイズなどの設定を再利用したいと思います。例えば、私は、この特定のプロットのための次の設定を設定した、と私は何とか将来図における使用のための「スタイル」として保存できるようにしたいと思います。特に将来のプロットでカスタム図の設定を使用
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 2)
y = x**2
fig, ax = plt.subplots()
# text specific to this plot, but attributes common to all
ax.plot(x, y, label='$y = x^2$', linewidth=2)
ax.set_xlabel('$x$', fontsize=20)
ax.set_ylabel('$y$', fontsize=20)
ax.set_title('Graph of $y = x^2$', fontsize=20)
# common to all plots
ax.legend(loc='best')
ax.spines['bottom'].set_color('grey')
ax.spines['left'].set_color('grey')
ax.xaxis.label.set_color('grey')
ax.yaxis.label.set_color('grey')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.tick_params(colors='grey')
plt.show()
、用関数内の文字列はこの図に固有のものですが、linewidth=2
のような属性は将来の図を共有したいと思います。 # common to all plots
の下の行は、すべての将来の数字を共有したいと思う属性です。 plt.figure()
の引数として、使いやすさのためにこれを「スタイル」として保存する方法はありますか?
@ImportanceoOfBeingErnest大きな謝辞をありがとう。唯一の問題は、上軸と右軸のティックがまだ表示されていることです。どのようにこれらを取り除くためにどのようなアイデア? – bcf
私はmatplotlib 2.0でそれをテストし、うまくいきました。どのバージョンを使用していますか?あなたは私の答えからスニペットを正確にコピーしましたか?見た目のイメージを表示できますか(おそらくimgur.comなどにアップロードしてください)? – ImportanceOfBeingErnest
私はmatplotlibを2.0.0にアップグレードしました。再度、感謝します! – bcf