使用 regplot 调用图形对象时如何抑制 seaborn 输出
How to suppress seaborn output when recalling figure object with regplot
我正在尝试将数据绘制到 matplotlib 中的图形和相应的轴上,随着新工作的出现,回忆一下轴上有附加图的图形:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
x=np.arange(0,20)
y=2*x
fig,ax=plt.subplots()
ax.scatter(x,x)
ax.scatter(x,y)
fig
如果我使用 seaborn 的 regplot,这在 matplotlib 上工作得很好:
fig2,ax2=plt.subplots()
sns.regplot(x,x,ax=ax2,fit_reg=False)
sns.regplot(x,y,ax=ax2,fit_reg=False)
fig2
fig2 生成了我想要的图形,但 regplot 命令生成了一个空图形。有没有办法抑制 regplot 的空输出或让它显示更新的 ax2 而不调用 fig2?
regplot 不会生成空图。根据文档:
Understanding the difference between regplot() and lmplot() can be a
bit tricky. In fact, they are closely related, as lmplot() uses
regplot() internally and takes most of its parameters. However,
regplot() is an axes-level function, so it draws directly onto an axes
(either the currently active axes or the one provided by the ax
parameter), while lmplot() is a figure-level function and creates its
own figure, which is managed through a FacetGrid.
当我执行以下操作时:
fig2,ax2 = plt.subplots()
same_fig2 = sns.regplot(x,x,ax=ax2,fit_reg=False)
same_fig2.figure is fig2
>>> True
您似乎正在使用带有内联后端的 jupyter notebook。在某些情况下 regplot
会触发创建一个新图形,即使艺术家被添加到前一个图形中,这也会弄乱输出。我不知道为什么会这样,但我找到了一个可能对您有帮助的解决方法,使用 plt.ioff
暂时禁用数字的自动显示。
plt.ioff()
fig, ax = plt.subplots()
sns.regplot(x, x, ax=ax)
fig
sns.regplot(x, 2 * x, ax=ax)
fig
您必须调用 plt.ioff
before creating the figure for this to work. After that you have to explicitly display the figure. Then you can call plt.ion
才能恢复默认行为。
我正在尝试将数据绘制到 matplotlib 中的图形和相应的轴上,随着新工作的出现,回忆一下轴上有附加图的图形:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
x=np.arange(0,20)
y=2*x
fig,ax=plt.subplots()
ax.scatter(x,x)
ax.scatter(x,y)
fig
如果我使用 seaborn 的 regplot,这在 matplotlib 上工作得很好:
fig2,ax2=plt.subplots()
sns.regplot(x,x,ax=ax2,fit_reg=False)
sns.regplot(x,y,ax=ax2,fit_reg=False)
fig2
fig2 生成了我想要的图形,但 regplot 命令生成了一个空图形。有没有办法抑制 regplot 的空输出或让它显示更新的 ax2 而不调用 fig2?
regplot 不会生成空图。根据文档:
Understanding the difference between regplot() and lmplot() can be a bit tricky. In fact, they are closely related, as lmplot() uses regplot() internally and takes most of its parameters. However, regplot() is an axes-level function, so it draws directly onto an axes (either the currently active axes or the one provided by the ax parameter), while lmplot() is a figure-level function and creates its own figure, which is managed through a FacetGrid.
当我执行以下操作时:
fig2,ax2 = plt.subplots()
same_fig2 = sns.regplot(x,x,ax=ax2,fit_reg=False)
same_fig2.figure is fig2
>>> True
您似乎正在使用带有内联后端的 jupyter notebook。在某些情况下 regplot
会触发创建一个新图形,即使艺术家被添加到前一个图形中,这也会弄乱输出。我不知道为什么会这样,但我找到了一个可能对您有帮助的解决方法,使用 plt.ioff
暂时禁用数字的自动显示。
plt.ioff()
fig, ax = plt.subplots()
sns.regplot(x, x, ax=ax)
fig
sns.regplot(x, 2 * x, ax=ax)
fig
您必须调用 plt.ioff
before creating the figure for this to work. After that you have to explicitly display the figure. Then you can call plt.ion
才能恢复默认行为。