显示字典中的海洋情节 [英] Showing seaborn plot from dictionary
问题描述
我需要将图存储在这样的字典中:
I need to store plots in a dictionary like this:
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
中的图?
But how do I show that plot from the dict
?
test['a']
返回类似于<seaborn.axisgrid.FacetGrid at 0x233011bfe80>
的图对象,但不显示图本身.
test['a']
returns the plot object like <seaborn.axisgrid.FacetGrid at 0x233011bfe80>
but does not show the plot itself.
test['a']
plt.show()
也不显示图.
有人建议如何显示字典中的情节吗?
Any suggestions how to show the plot from the dictionary?
基于给出的答案,我尝试将dict
保存到一个pickle文件中,但是显示pickle中的图会出现错误:
Based on the answer given I tried saving the dict
to a pickle file, but showing the plot from pickle gives an error:
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
推荐答案
调用相关的fig
似乎足够了:
It seems to suffice to call the associated fig
:
test['a'].fig
在Jupyter笔记本中为我工作.
works for me in a jupyter notebook.
btw:必须删除高度参数
btw: had to remove height parameter
完全改变问题不是很好.但是,看来您的问题仅与木星有关! 选中此问题.
It's not good to change the question entirely. However, it seems that your issue is only related to Jupiter! Check this issue.
一种解决方案是在第一个单元格前添加%matplotlib notebook
One solution is to prepend the first cell with %matplotlib notebook
这篇关于显示字典中的海洋情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!