gpt4 book ai didi

python - Matplotlib - 只显示一个数字

转载 作者:太空狗 更新时间:2023-10-29 22:11:04 26 4
gpt4 key购买 nike

所以我得到了一些我的代码生成的图。我想保存所有,但我只想显示一个。我可以很好地完成保存部分,但只显示一个是很痛苦的。

为了实现这个目标一直在兜圈子。我得到的最接近的是清除除了一个数字之外的所有数字,但是当我试图只显示一个数字时它们都显示出来了。开始认为这要么是我忽略的非常简单的事情,要么是不可能的。

有人知道如何实现吗?

编辑:添加示例代码。很抱歉最初没有这样做。

    fig1 = plt.figure(1)
plt.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], label="Test", color='g')
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], label="Other Test", color='r')
plt.grid(True)

fig1.savefig('Foo1.png')

fig2 = plt.figure(2)
plt.plot([0, 1, 2, 3, 4], [0, 5, 1, 9, 2], label="Test 2", color='g')
plt.plot([0, 1, 2, 3, 4], [0, 10, 50, 0, 10], label="Other Test 2", color='r')
plt.grid(True)

fig2.savefig('Foo2.png')

plt.show()

最佳答案

您可以在使用 plt.close() 保存后立即关闭每个图。请确保不要在最后一个数字之后做出结束声明。

您的代码将如下所示:

fig1 = plt.figure(1)
plt.plot([0, 1, 2, 3, 4], [0, 1, 2, 3, 4], label="Test", color='g')
plt.plot([0, 1, 2, 3, 4], [0, 1, 4, 9, 16], label="Other Test", color='r')
plt.grid(True)

fig1.savefig('Foo1.png')
# add plt.close() after you've saved the figure
plt.close()

fig2 = plt.figure(2)
plt.plot([0, 1, 2, 3, 4], [0, 5, 1, 9, 2], label="Test 2", color='g')
plt.plot([0, 1, 2, 3, 4], [0, 10, 50, 0, 10], label="Other Test 2", color='r')
plt.grid(True)

fig2.savefig('Foo2.png')

plt.show()

关于python - Matplotlib - 只显示一个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com