3 回答
TA贡献2012条经验 获得超12个赞
我不确定我可以给你一个适用于你现有代码的例子,但我是这样做的:
Figure = None
def invoice_chart_clicked (self, button):
global Figure
if Figure == None:
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.pyplot import pie
self.figure = Figure(figsize=(4, 4), dpi=100)
canvas = FigureCanvas(self.figure) # a Gtk.DrawingArea
canvas.set_size_request(800, 500)
overlay = self.builder.get_object('overlay1')
overlay.add (canvas)
a = self.figure.add_subplot(111)
labels = list()
fractions = list()
unpaid = 0
self.cursor.execute("SELECT SUM(amount_due), c.name FROM invoices "
"JOIN contacts AS c ON c.id = invoices.customer_id "
"WHERE (canceled, paid, posted) = "
"(False, False, True) GROUP BY customer_id, c.name "
"ORDER BY SUM(amount_due)")
for row in self.cursor.fetchall():
customer_total = row[0]
customer_name = row[1]
fractions.append(customer_total)
labels.append(customer_name)
unpaid += 1
if unpaid == 0:
labels.append("None")
fractions.append(1.00)
a.pie(fractions, labels=labels, autopct='%1.f%%', radius=0.7)
window = self.builder.get_object('window1')
window.show_all()
每次我重新加载这个函数时,绘图都会重新生成。您可以在此处找到完整代码。我从来没有运行过任何测试来查看所有内存是否都被正确释放等等。也许它可以从那里开始。
添加回答
举报