Figure of piechart empty

时间:2016-01-12 11:21:55

标签: python matplotlib tkinter

I created a piechart in matplot lib, when I show it by using plt.show() it will be displayed correctly. But when I want to add it to my tkinter frame it will display an empty plot.

labels = [r'Rayos X (88.4 %)', r'RMN en solucion (10.6 %)', r'Microscopia electronica (0.7 %)', r'Otros (0.3 %)']
sizes = [88.4, 10.6, 0.7, 0.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.legend(patches, labels, loc="best")
# Set aspect ratio to be equal so that pie is drawn as a circle.
plt.axis('equal')
plt.tight_layout()
# plt.show()

fig = plt.figure()
canvas = FigureCanvasTkAgg(fig, master=self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
canvas.get_tk_widget().place(x=0, y=40, width=800, height=560)
canvas.get_tk_widget().configure(background='lightgray', highlightcolor='lightgray', highlightbackground='lightgray')

1 个答案:

答案 0 :(得分:1)

在创建饼图之前,您需要在图中添加一个子图:

root = Tk.Tk()
root.wm_title("Pie Chart")

labels = [r'Rayos X (88.4 %)', r'RMN en solucion (10.6 %)', r'Microscopia electronica (0.7 %)', r'Otros (0.3 %)']
sizes = [88.4, 10.6, 0.7, 0.3]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']

fig = plt.figure()
piechart = fig.add_subplot(1, 1, 1)
patches, texts = piechart.pie(sizes, colors=colors, startangle=90, pctdistance=1)
piechart.legend(patches, labels, loc="best")
piechart.axis('equal')

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
Tk.mainloop()