我正在构建一个需要输入的应用程序,将其保存到CSV文件,现在最后一个基本部分是显示该列内容的饼图。输入应该是关于书籍的。因此,饼图应例如显示列表中所有书籍的类型。
我在创建饼图方面没有问题,我在单独的脚本中对其进行了管理,我也未收到任何错误消息,但是只有白色字段。
import csv
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import ttk
from collections import Counter
class FrontFrames:
def __init__(self, master):
super().__init__()
#Split the screen horizontally in two parts
top_frame = tk.Frame(master, bg="green")
bottom_frame = tk.Frame(master, bg="yellow")
top_frame.pack(side="top", fill="both", expand=True)
bottom_frame.pack(side="top", fill="both", expand=True)
#Creating the three top widgets
self.tree = Label(top_frame)
column = ("Title", "author", "year", "others")
self.treeview = ttk.Treeview(top_frame, columns=column, show='headings')
self.treeview.heading("Title", text="Title")
self.treeview.heading("author", text="Author")
self.treeview.heading("year", text="Year")
self.treeview.heading("others", text="Others")
content = "./Note.csv"
with open(content, 'r') as file:
csv_reader = csv.DictReader(file, skipinitialspace=True)
for row in csv_reader:
self.treeview.insert('', '0', values=(f'{row["title"]}',
f'{row["author"]}',
f'{row["year"]}',
f'{row["others"]}'))
c = Counter(row["others"])
header = list(c.keys())
values = list(c.values())
colors = ['r', 'g', 'b', 'c', 'y']
f = plt.Figure(figsize=(4,4))
#a = f.add_subplot(111)
pie = plt.pie(values, labels=header, colors=colors, startangle=90,
autopct='%.1f%%')
self.canvas = FigureCanvasTkAgg(f, top_frame)
self.tree.pack(side="left", fill="both", expand=True)
self.treeview.pack(side="left", fill="both", expand=True)
self.canvas.get_tk_widget().pack(side="right", fill="both")
#self.canvas.show()
我将“ self.canvas.show()”行注释掉,因为否则会收到错误消息:
AttributeError:'FigureCanvasTkAgg'对象没有属性'show'
通常会告诉您必须包含该命令才能实际显示饼图,但是在没有构建其他小部件的单独测试中,饼图是可见的。我认为饼图只是隐藏在另一个小部件下。
CSV的内容如下:
title, author, year, others, note
Everything,Nobody,2222,Fiction,This is just something of everything!
Nothing,Everybody,1111,Romance,This is nothing of anything!
Pokemon,Pikachu,1999,Fiction,Once upon time.
Lord of the rings, R.R. Jackson, 1972, Fantasy, A long story!
Harry Potter, Rowling, 1995, Fantasy, A detailed story.
Bible, Apostels, 0, Fiction, A story.
The Ring, Frank, 1980, Horror, A scary story.
1984, Orwell, 1932, Dystopia, A scary future for the past and scary present
for us.
Pirates of the Caribbean, Brandow, 1955, Fiction, A pirate story.
我使用Python 3.7.0
一如既往,非常感谢您提供有用的答案。
答案 0 :(得分:1)
对于....
ORDER BY CONVERT(DATE,'01-'+ DateAsStringColumn ,103);
,我的python解释器告诉我self.canvas.show
已过时,我应该改用FigureCanvasTkAgg.show
。
对于图的问题,我通过取消注释draw
并替换为
a = f.add_subplot(111)
通过
plt.pie(values, labels=header, colors=colors, startangle=90, autopct='%.1f%%')
所以我的猜测是,由于您没有明确指定坐标轴,所以您的图表显示错误的图形。