当我尝试使用seaborn保存我的情节时,像这样:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
from pylab import savefig
array = [[100,0],
[33,67]]
df_cm = pd.DataFrame(array)
svm = sn.heatmap(df_cm, annot=True,cmap='coolwarm', linecolor='white', linewidths=1)
svm.savefig('svm_conf.png', dpi=400)
我收到此错误
AttributeError Traceback (most recent call last)
<ipython-input-71-5c0ae9cda020> in <module>()
----> 1 svm.savefig('svm_conf.png', dpi=400)
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
我之前用相同的代码保存了一些箱图,但这一次,它不起作用。
答案 0 :(得分:11)
实际上你需要做的是:
sn.heatmap
见下面最后两行:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
from pylab import savefig
array = [[100,0],
[33,67]]
df_cm = pd.DataFrame(array)
svm = sn.heatmap(df_cm, annot=True,cmap='coolwarm', linecolor='white', linewidths=1)
figure = svm.get_figure()
figure.savefig('svm_conf.png', dpi=400)
答案 1 :(得分:2)
最简单的方法是使用
plt.savefig('svm_conf.png', dpi=400)
而不是
svm.savefig('svm_conf.png', dpi=400)
答案 2 :(得分:2)
保存当前数字的命令是
plt.savefig()
因为这显然会引起一些混乱,所以这是完整的工作示例:
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
array = [[100,0],
[33,67]]
df_cm = pd.DataFrame(array)
svm = sn.heatmap(df_cm, annot=True,cmap='coolwarm', linecolor='white', linewidths=1)
plt.savefig('svm_conf.png', dpi=400)