在熊猫中绘图的奇怪行为
我想知道是否期望以下代码的行为。 如我所期望的那样,第一个数字(系列)被保存。第二个(数据框)不是。 如果这不是错误,我该如何实现(明显)目标?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
保存图后,使用
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 useplt.close('all')
to be sure all open figures are closed.