2016-01-12 107 views
2

我在matplot lib中創建了一個餅圖,當我用plt.show()顯示它時,它會正確顯示。但是當我想將它添加到我的tkinter框架時,它會顯示一個空的圖。餅圖空圖

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

你需要一個插曲添加到圖中,它創建一個餅圖前:

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() 
+0

非常感謝,你幫了我 –

+0

沒問題。如果您想更多地閱讀這些概念,請考慮以下鏈接:http://matplotlib.org/faq/usage_faq.html –