python 使用Matplotlib 画图 显示在tkinter 中的方法

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

在这里插入图片描述

  • 13
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值