在熊猫中绘图的奇怪行为

发布于 2025-01-31 21:57:36 字数 374 浏览 2 评论 0原文

我想知道是否期望以下代码的行为。 如我所期望的那样,第一个数字(系列)被保存。第二个(数据框)不是。 如果这不是错误,我该如何实现(明显)目标?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')

fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')

I would like to know if the behavior of the following code is expected.
The first figure (Series) is saved as I would expect. The second (DataFrame) is not.
If this is not a bug, how can I achieve my (obvious) goal?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')

fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2025-02-07 21:57:36

保存图后,使用plt.close()关闭当前图以关闭当前图,否则即使下一个绘图正在生成,旧的图仍然处于活动状态。您也可以使用plt.close('all')确保关闭所有打开数字。

After saving the figure, close the current plot using plt.close() to close the current figure, otherwise the old one is still active even if the next plot is being generated. You can also use plt.close('all') to be sure all open figures are closed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文