matplotlibとseabornを使って作成したfigureを保存する。
言語:Python
matplotlibとseabornを使って作成したfigureを保存しようとしています。
IPython上でplt.showした際の図は望み通りの出力になっていますが、plt.savefigした際の図はタイトルや軸ラベルが消えてしまいます。
発生している問題・エラーメッセージ
該当のソースコード
Python
1import numpy as np 2import pandas as pd 3from matplotlib import pyplot as plt 4import seaborn as sns 5 6sns.set() 7sns.set_style('whitegrid') 8sns.set_palette('Set3') 9 10np.random.seed(2018) 11 12df = pd.DataFrame({ 13 'leaf': np.random.normal(10, 2, 20), 14 'stem': np.random.normal(15, 3, 20), 15 'root': np.random.normal(5, 1, 20) 16}) 17 18df_melt = pd.melt(df) 19 20fig = plt.figure() 21ax = fig.add_subplot(1, 1, 1) 22#sns.boxplot(x='variable', y='value', data=df_melt, showfliers=False, ax=ax) 23sns.barplot(x='variable', y='value', data=df_melt, ax=ax) 24sns.stripplot(x='variable', y='value', data=df_melt, jitter=True, color='#e41a1c', ax=ax) 25ax.set_title('Title') 26plt.ylim(0,1) 27plt.savefig('test.png') 28plt.show()
試したこと
plt.savefig('test.png')
を
plt.savefig('test.png', bbox_inches='tight')
に変更する。
補足情報(FW/ツールのバージョンなど)
Python (3.6.8 64bit 'base':conda)、OS:macOS10.14.4、エディタ:vs code 1.35.1
回答1件
あなたの回答
tips
プレビュー