[英]How do I save a plt figure?
我正在尝试保存由下面的代码产生的数字。 我不断收到错误。
x = range(-300,1)
x_2 = range(0,301)
y = range(-300,1)
y_2 = range(0,301)
x_1 = range(-300,1)
y_3 = range(0,-301,-1)
x_3 = range(0,-301,-1)
x_0 = [0 for i in range(301)]
y_0 = [0 for i in range(301)]
plt.plot(x_2,y_2, label="direction 1")
plt.plot(x_2,y_3, label="direction 2")
plt.plot(x_1,y, label="direction 3")
plt.plot(x_3,y_2, label="direction 4")
plt.plot(x_0,y_2, label="direction 5")
plt.plot(x_0,y, label="direction 6")
plt.plot(x_2,y_0, label="direction 7")
plt.plot(x,y_0, label="direction 8")
plt.legend(loc="best")
plt.xlabel("x")
plt.ylabel("y");
我试过命名其中一行代码并保存它,但它说列表没有属性 savefig。 所有这些线都应该绘制在一个图中。
如果您想事先查看图表,请使用plt.savefig()
或plt.show()
然后按“s”
在代码末尾添加plt.savefig("test.png")
并在开头import matplotlib.pyplot as plt
,在我的计算机上创建一个完全有效的图片:
这是完整的脚本:
import matplotlib.pyplot as plt
x = range(-300, 1)
x_2 = range(0, 301)
y = range(-300, 1)
y_2 = range(0, 301)
x_1 = range(-300, 1)
y_3 = range(0, -301, -1)
x_3 = range(0, -301, -1)
x_0 = [0 for i in range(301)]
y_0 = [0 for i in range(301)]
plt.plot(x_2, y_2, label="direction 1")
plt.plot(x_2, y_3, label="direction 2")
plt.plot(x_1, y, label="direction 3")
plt.plot(x_3, y_2, label="direction 4")
plt.plot(x_0, y_2, label="direction 5")
plt.plot(x_0, y, label="direction 6")
plt.plot(x_2, y_0, label="direction 7")
plt.plot(x, y_0, label="direction 8")
plt.legend(loc="best")
plt.xlabel("x")
plt.ylabel("y")
plt.savefig("test.png")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.