我需要将绘图存储在一个字典中,格式如下:
import matplotlib.pyplot as plt
import seaborn as sns
test = dict()
test['a'] = sns.lmplot(x="sepal_length", y="sepal_width", hue="species",
truncate=True, height=5, data=sns.load_dataset("iris"))
但是我该如何从 dict
中显示这个图表呢?
test['a']
返回的是类似于 <seaborn.axisgrid.FacetGrid at 0x233011bfe80>
的绘图对象,但并不会显示图表本身。
test['a']
plt.show()
同样也没有显示图表。
有什么建议可以从字典中显示图表吗?
根据给出的答案,我尝试将dict
保存到pickle文件中,但是从pickle中显示图表时会出现错误:
import pickle
import matplotlib.pyplot as plt
import seaborn as sns
test = dict()
test['a'] = sns.lmplot(x="sepal_length", y="sepal_width", hue="species",
truncate=True, data=sns.load_dataset("iris"))
plt.close()
# store dict in pickle file
outfile = open('test.pkl', 'wb')
pickle.dump(test, outfile)
outfile.close()
# delete dictionary
del test
# load dictionary form pickle file
infile = open('test.pkl', 'rb')
test = pickle.load(infile)
infile.close()
test['a'].fig