使用 SeabornFig2Grid 绘制多个 Seaborn 图形时出错
Error plotting multiple Seaborn figures using SeabornFig2Grid
我画了 4 个 Seaborn 图形,我想使用 SeabornFig2Grid 将其放在一个图形上,如本 answer 中所建议:
#%%
fig5 = sns.regplot(plotdata['Average precipitation in depth (mm per year)'],plotdata['Lifetime risk of maternal death (%)'], data=plotdata)
fig5 = sns.set(font_scale=1.4)
fig5 = plp.annotate('r-square = {0:.2f}'.format(r_value**2), (0.05, 0.8), xycoords='axes fraction')
fig5 = plp.annotate('y = {0:.2f} + {0:.2f} x Average precipitation in depth (mm/year)'.format(intercept1, slope1), (0.05, 0.9), xycoords='axes fraction')
fig5 = plt.gcf()
fig5.set_size_inches(10, 5)
fig6 = ....
fig7 = ....
fig8 = ....
fig = plt.figure(figsize=(45,25))
gs = gridspec.GridSpec(2, 2)
mg0 = sfg.SeabornFig2Grid(fig5, fig, gs[0])
mg1 = sfg.SeabornFig2Grid(fig6, fig, gs[1])
mg2 = sfg.SeabornFig2Grid(fig7, fig, gs[2])
mg3 = sfg.SeabornFig2Grid(fig8, fig, gs[3])
gs.tight_layout(fig9)
#fig.savefig('fig9.jpg')
我通过改编为 SeabornFig2Grid 提供的示例代码编写了我的代码,但是 returns 出现以下错误:
File "<ipython-input-27-d3c8f9b3c3ea>", line 32, in <module>
mg0 = sfg.SeabornFig2Grid(fig5, fig9, gs[0])
File "C:\Anaconda\lib\site-packages\SeabornFig2Grid.py", line 17, in __init__
self._finalize()
File "C:\Anaconda\lib\site-packages\SeabornFig2Grid.py", line 52, in _finalize
plt.close(self.sg.fig)
AttributeError: 'Figure' object has no attribute 'fig'
我的代码有什么问题?
sns.regplot
不是 return 图形,而是轴。对于 regplot
你不需要使用 SeabornFig2Grid
class。相反,您可以直接将其绘制到图形的轴上。
fig = plt.figure(figsize=(45,25))
gs = gridspec.GridSpec(2, 2)
ax5 = fig.add_subplot(gs[0])
sns.regplot(..., ax=ax5)
我画了 4 个 Seaborn 图形,我想使用 SeabornFig2Grid 将其放在一个图形上,如本 answer 中所建议:
#%%
fig5 = sns.regplot(plotdata['Average precipitation in depth (mm per year)'],plotdata['Lifetime risk of maternal death (%)'], data=plotdata)
fig5 = sns.set(font_scale=1.4)
fig5 = plp.annotate('r-square = {0:.2f}'.format(r_value**2), (0.05, 0.8), xycoords='axes fraction')
fig5 = plp.annotate('y = {0:.2f} + {0:.2f} x Average precipitation in depth (mm/year)'.format(intercept1, slope1), (0.05, 0.9), xycoords='axes fraction')
fig5 = plt.gcf()
fig5.set_size_inches(10, 5)
fig6 = ....
fig7 = ....
fig8 = ....
fig = plt.figure(figsize=(45,25))
gs = gridspec.GridSpec(2, 2)
mg0 = sfg.SeabornFig2Grid(fig5, fig, gs[0])
mg1 = sfg.SeabornFig2Grid(fig6, fig, gs[1])
mg2 = sfg.SeabornFig2Grid(fig7, fig, gs[2])
mg3 = sfg.SeabornFig2Grid(fig8, fig, gs[3])
gs.tight_layout(fig9)
#fig.savefig('fig9.jpg')
我通过改编为 SeabornFig2Grid 提供的示例代码编写了我的代码,但是 returns 出现以下错误:
File "<ipython-input-27-d3c8f9b3c3ea>", line 32, in <module>
mg0 = sfg.SeabornFig2Grid(fig5, fig9, gs[0])
File "C:\Anaconda\lib\site-packages\SeabornFig2Grid.py", line 17, in __init__
self._finalize()
File "C:\Anaconda\lib\site-packages\SeabornFig2Grid.py", line 52, in _finalize
plt.close(self.sg.fig)
AttributeError: 'Figure' object has no attribute 'fig'
我的代码有什么问题?
sns.regplot
不是 return 图形,而是轴。对于 regplot
你不需要使用 SeabornFig2Grid
class。相反,您可以直接将其绘制到图形的轴上。
fig = plt.figure(figsize=(45,25))
gs = gridspec.GridSpec(2, 2)
ax5 = fig.add_subplot(gs[0])
sns.regplot(..., ax=ax5)