Matplotlib是唯一与Tkinter兼容的绘图软件吗?

2024-05-23 13:19:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为我的笔记本电脑创建自己的Gnome电源统计数据,并使用Tkinter在长期历史视图之间切换:

battery long term.png

短期观点:

battery short term.png

不同的是,我将收集更多和更少的电池数据点,并提供更多的开始/结束时间。此外,它还会在每周三晚上用智能插头打开/关闭电池充电器。你知道吗

在堆栈溢出中搜索后,所有道路似乎都指向Matplotlib,使用:

import matplotlib.pyplot as plt 

这是唯一的选择,还是有其他开源库可以生成如上所示的动态图?你知道吗

我特别喜欢这个软件,它在这里有很多问题和答案,比麻省理工学院便宜73160美元/年。你知道吗


Tags: 数据视图电池堆栈智能tkinter时间历史
1条回答
网友
1楼 · 发布于 2024-05-23 13:19:53

在查看提供的图像时,您需要的函数绘图似乎非常基本:背景虚线网格(似乎是恒定的)、垂直比例(似乎也是恒定的)、水平比例(取决于所选的周期)以及绘图本身的一组线段。这可以在纯tkinter中使用Canvas小部件轻松实现。你可以试试。。。你知道吗

编辑(在您的评论之后):下面是一个示例代码,它在虚线网格上绘制了三条曲线(阻尼余弦波及其两条阻尼钟形曲线)。我想这可能是一个很好的起点,了解一般过程:

from tkinter import *

def draw_curve():
    from math import cos, exp
    w, h, colors = width//2, height//2, ('#0F0','#F00','#00F')
    for n in range(3): # loop over curves
        xa, ya, xb, yb = 0, h, 0, h # initial position for each curve
        for x in range(w+1): # loop over horizontal axis
            t = 2*x/w - 1 # parameter t moves over range [-1,1]
            if n == 2: # draw damped cosine wave
                xa, ya, xb, yb = xb, yb, 2*x, h + h*exp(-5*t*t)*cos(25*t)
            elif n == 1: # draw negative bell curve
                xa, ya, xb, yb = xb, yb, 2*x, h + h*exp(-5*t*t)
            elif n == 0: # draw positive bell curve
                xa, ya, xb, yb = xb, yb, 2*x, h - h*exp(-5*t*t)
            canvas.create_line(xa, ya, xb, yb, width=1, fill=colors[n])    

def draw_grid():
    steps = 20 # number of grid steps
    dw, dh = width/steps, height/steps # horizontal and vertical steps
    for n in range(steps):
        canvas.create_line(0, n*dh, width, n*dh, width=1, dash=(1,1)) 
        canvas.create_line(n*dw, 0, n*dw, height, width=1, dash=(1,1))
    canvas.create_rectangle(2, 2, width-1, height-1, width=1)

width, height = 800, 600
win = Tk()
canvas = Canvas(win, width=width, height=height)
canvas.pack(padx=5, pady=5)
draw_grid()
draw_curve()
win.mainloop()

相关问题 更多 >