import xlrd
import openpyxl
from tkinter import filedialog #路径选择
from tkinter import *
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import numpy as np
root = Tk() # 创建tkinter的主窗口
root.title("练习窗口")
root.geometry('320x240+10+10')
#标签
label1=Label(root,text='测试窗口')
label1.place(x=0,y=0)
#文本框
Text = Text(root,width = 50,height=10)#测试文本框
Text.place(x=0,y=30)
#图像及画布
fig = plt.figure(figsize=(10,4),dpi=100)#图像比例
f_plot =fig.add_subplot(111)#划分区域
canvas_spice = FigureCanvasTkAgg(fig,root)
canvas_spice.get_tk_widget().place(x=0,y=200)#放置位置
def get_xl():
file = filedialog.askopenfilename()#选择路径
read = xlrd.open_workbook(file)#打开文件
sheet1 = read.sheet_by_index(0)#打开第一个表格
nrows = sheet1.nrows#总行数
ncols = sheet1.ncols#总列数
a=2
b=0
x1 =[]
y1 =[]
#数据处理
for i in range(nrows):
for j in range(ncols):
ctype = sheet1.cell(i,j).ctype
#检查表格数据类型,0 空值 1 字符串 2数字 3日期 4布尔 5 错误
if ctype ==1:
value = sheet1.cell_value(i,j)#cell_value方法取出第i行j列的数据
a+=1
b+=1
if a>5 and a%3==0:
value2 = sheet1.cell_value(i,j)
y=(value2.strip('Ang= °'))
y1.append(float(y))
if b%3==0:
value3 = sheet1.cell_value(i,j)
x1.append(value3[10:])
#if value <= 1:
#value1=value+39274.0 #补全时间戳
#date = datetime(*xldate_as_tuple(value1,0))
#value = date.strftime('%H:%M:%S')
if ctype ==0:
break
#print(x1,y1)
#画图
plt.rcParams['font.sans-serif']='SimHei'#全局字体
f_plot.clear()#刷新
plt.xlabel('日期',size = 20)
plt.ylabel('天顶角(单位°)',size =20)
plt.title('N20 卫星 5—6月,天顶角情况',size = 20)
plt.axis([0,30,0,90])
plt.plot(x1,y1)
plt.grid(True)#网格
canvas_spice.draw()
#按钮
button = Button(root,text='选择文件',bg='lightblue',width=10,command=get_xl)
button.place(x=400,y=1)
# 主循环
root.mainloop()
python 使用Matplotlib 画图 显示在tkinter 中的方法
最新推荐文章于 2024-08-07 22:58:53 发布