pltでgnuplotみたいに図を書きたいと思ったので,色々設定をいじって近づけてみた.いちいち設定するのは多少面倒かもしれないが,pltの設定はかゆいところに手が届くと思う.しかしながら,手の込んだ図はseabornに軍配が上がりそう.
あと,gnuplotでは第2x,y軸をmirror
で簡単に処理できるところを,pythonでは少し面倒.pltがよく使われるコマンドならもっと簡単にできるように実装されてると思う.そもそも,gnuplotでmirror
を設定している研究室は稀なのかもしれない.
ソース
gnuplotの散布図風の図
plt.rcParams['font.family'] = 'Times New Roman' # font familyの設定
plt.rcParams['mathtext.fontset'] = 'stix' # math fontの設定
plt.rcParams["font.size"] = 15 # 全体のフォントサイズが変更されます。
plt.rcParams['xtick.labelsize'] = 24 # 軸だけ変更されます。
plt.rcParams['ytick.labelsize'] = 24 # 軸だけ変更されます
plt.rcParams['figure.dpi'] = 300
plt.rcParams['xtick.direction'] = 'in' # x axis in
plt.rcParams['ytick.direction'] = 'in' # y axis in
plt.rcParams["legend.fancybox"] = False # 丸角
plt.rcParams["legend.edgecolor"] = 'black' # edgeの色を変更
plt.rcParams["legend.labelspacing"] = 0.9 # 垂直方向の距離の各凡例の距離
plt.rcParams["legend.framealpha"] = 1 # 透明度の指定、0で塗りつぶしなし
plt.rcParams["legend.handlelength"] = 2 # 凡例の線の長さを調節
fig, ax = plt.subplots(figsize=(8,6))
# plt.subplots_adjust(left=0.1, right=1, bottom=0, top=1.03) # 図の余白調整
ax.legend(loc='best')
ax2 = ax.twinx() # ax2 handles y
ax3 = ax2.twiny() # ax3 handles x
ax2.set_yticks(ax.get_yticks())
ax2.set_ylim(ax.get_ylim())
ax3.set_xticks(ax.get_xticks())
ax3.set_xlim(ax.get_xlim())
ax2.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)
ax3.tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False)
# epsも可能.ただし,これで作成したepsはtexに挿入するとズレを引き起こす可能性あり.そのため,png作成→ブラウザソフトやInkscapeなどでeps変換を推奨.
fig.savefig("file.png", bbox_inches="tight", pad_inches=0.05,transparent=True)
参考