简体   繁体   English

How to display matplotlib graph extract from excel file in tkinter windows python

[英]How to display matplotlib graph extract from excel file in tkinter windows python

I would like to use my data from excel to draw a graph and display it on Tkinter windows.我想使用 excel 中的数据绘制图形并将其显示在 Tkinter windows 上。 but I couldn't and I don't know how I can do it I search in many places I got only examples to show data from the code.但我不能,我不知道我该怎么做我在很多地方搜索我只有示例来显示代码中的数据。 here is my code:这是我的代码:

here the file of Excel download here Excel的文件在这里下载

import tkinter as tk
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

df= pd.read_excel (r'C:\Users\evil4ever\Desktop\acheraf\project\projetCorona.xlsx') 
tab1=df[0:31]

#Dates=['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
Dates = []
for i in range(1,32):
    Dates.append(str(i))


Values=tab1['Cas confirmés']#rmorocco
Values2=tab1['RABAT.SALE.KENITRA']#rabat

f=plt.Figure(figsize=(9,5), dpi=100)

plt.xlabel('days of March')
plt.ylabel('Confirmed cases')
plt.title('Confirmed Cases in March RABAT.SALE.KENITRA ')
plt.bar(Dates,Values,label='all morocco',color='r')
plt.bar(Dates,Values2,label='RABAT.SALE.KENITRA',color='c')
a=f.add_subplot(111)
root= tk.Tk()
canvas=FigureCanvasTkAgg(f, root)

canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
root.mainloop()

explain the problem: when i run the program is showing this windows look this image解释问题:当我运行程序时显示这个 windows 看这个图像

and the graph is showing here in this image after the code showing the graph which I wanna display on windows not there并且在显示我想在 windows 上显示的图形的代码之后,该图形在此图像中显示

after this line:a=f.add_subplot(111) i add a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5]) is showing something in windows在此行之后:a=f.add_subplot(111) 我添加 a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3 ,5]) 在 windows 中显示某些内容

i search to much for 3 days without any solution.我在没有任何解决方案的情况下搜索了 3 天。

Use Figure instead of pyplot when combining matplotlib with tkinter .matplotlibtkinter结合使用时,请使用Figure而不是pyplot See this for an official sample.请参阅获取官方示例。

import tkinter as tk
import pandas as pd
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

df = pd.DataFrame({"Name":list("ABCD"),"Value":[1,2,3,4,]})
#df = pd.read_excel (r'C:\Users\evil4ever\Desktop\acheraf\project\projetCorona.xlsx') 

f = Figure(figsize=(9,5), dpi=100)

ax = f.add_subplot(111)

ax.set_xlabel('days of March')
ax.set_ylabel('Confirmed cases')
ax.set_title('Confirmed Cases in March RABAT.SALE.KENITRA')

df.plot(kind="bar",ax=ax)

root= tk.Tk()
canvas=FigureCanvasTkAgg(f, root)

canvas.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM