我试图在Canvas中显示如数组中所示的折线图(字符串和数字)。我在这个论坛中从不同的问题得到了这个代码,试图修改我的要求。可以请一些人指导我。
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({"Week": ['Week1','Week2','Week3','Week4','Week5'],
"App1" : [2.6,3.4,3.25,2.8,1.75],
"App2" : [2.5,2.9,3.0,3.3,3.4],
"App3" : [1.6,2.4,1.25,5.8,6.75]})
df.plot(x="Week", y=["App1", "App2", "App3"])
plt.show()
答案 0 :(得分:0)
如果其他人正在寻找的话,这是一个。
# --- matplotlib ---
import matplotlib
matplotlib.use('TkAgg') # choose backend
from tkinter import messagebox
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.pyplot import Figure
from matplotlib import pyplot as plt
# --- other ---
import tkinter as tk
import pandas as pd
# --- GUI ---
root = tk.Tk()
# top frame for canvas and toolbar - which need `pack()` layout manager
top = tk.Frame(root)
top.pack()
# bottom frame for other widgets - which may use other layout manager
bottom = tk.Frame(root)
bottom.pack()
# create figure
fig = matplotlib.pyplot.Figure()
# create matplotlib canvas using `fig` and assign to widget `top`
canvas = FigureCanvasTkAgg(fig, top)
# get canvas as tkinter widget and put in widget `top`
canvas.get_tk_widget().pack()
canvas._tkcanvas.pack()
# --- plot ---
data = {"Week": ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],
"App1" : [2.6,3.4,3.25,2.8,1.75,5,2],
"App2" : [2.5,2.9,3.0,3.3,3.4,5,3],
"App3" : [1.6,15,1.25,5.8,6.75,6,4]
}
new_df = pd.DataFrame(data)
ax = fig.add_subplot(111)
fig.suptitle('Graph Title', fontsize=12)
new_df.plot(x="Week", y=["App1", "App2", "App3"],ax=ax)
def on_closing():
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.lift()
root.attributes('-topmost',True)
root.mainloop()