【发布时间】:2011-03-14 14:23:32
【问题描述】:
我有一个类,我用它来绘制东西然后将它们保存到一个文件中。这是它的简化版本:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class Test():
def __init__(self, x, y, filename):
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x, y, 'D', color='red')
ax.set_xbound(-5,5)
ax.set_ybound(-5,5)
plt.savefig('%s.png' % filename)
test1 = Test(1,2, 'test1')
test2 = Test(2,4, 'test2')
结果如下:
测试1
测试2
问题是 test2 图像也有来自 test1 的点。这些图表是在循环中动态生成的,因此我无法对数字进行硬编码。
我可以制作一个计数器并将其传递给类构造函数,但我想知道是否有更优雅的方法来做到这一点。我尝试删除 test1 对象,但没有做任何事情。
【问题讨论】:
标签: python matplotlib