画图时使用的函数和一些错误处理

1. 关于 画图时后的数据量匹配错误(应该是)

然后这块还有个问题:

python 复制代码
import pynvml
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def get_gpu_memory_usage(handle):
    gpu_mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
    return gpu_mem.used / 1024 ** 2  # Convert memory usage to MB

def animate(i, ys, handle):
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_ydata(ys)
    return line,

if __name__ == "__main__":
    pynvml.nvmlInit()
    handle = pynvml.nvmlDeviceGetHandleByIndex(0)  # Assuming you have only one GPU

    fig, ax = plt.subplots()
    line, = ax.plot([], [])
    ax.set_xlim(0, 100)
    ax.set_ylim(0, 10000)  # Assuming the maximum GPU memory is 10000 MB
    ax.set_title('Real-time GPU Memory Usage')
    ax.set_xlabel('Time')
    ax.set_ylabel('Memory Usage (MB)')

    ys = [0] * 100

    ani = animation.FuncAnimation(fig, animate, fargs=(ys, handle), interval=1000)

    plt.show()

    pynvml.nvmlShutdown()

出现这个错:"ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (0,) and arg 1 with shape (100,)."

把:

python 复制代码
def animate(i, ys, handle):
    # update GPU memory usage
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_ydata(ys)
    return line,

改为:

python 复制代码
def animate(i, ys, handle):
    ys.append(get_gpu_memory_usage(handle))
    ys = ys[-100:]
    line.set_data(range(len(ys)), ys)  # Set both x and y data
    ax.set_xlim(0, len(ys))  # Adjust x-axis limit based on the length of ys
    return line,    # 用于指示函数应返回包含 line 值的元组。
                    # 在 Python 中,单项元组必须有一个尾随逗号,以将其与括号内的普通表达式区分开。

就不报错

相关推荐
2401_8732046521 分钟前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
代码探秘者21 分钟前
【大模型应用】1.了解RAG
java·人工智能·python·spring
m0_7166670726 分钟前
工具、测试与部署
jvm·数据库·python
2501_9454248033 分钟前
构建一个基于命令行的待办事项应用
jvm·数据库·python
cowice1 小时前
Python基础知识
python
阿_旭1 小时前
基于YOLO26深度学习的铁轨部件缺陷检测与语音提示系统【python源码+Pyqt5界面+数据集+训练代码】
人工智能·python·深度学习·铁轨部件缺陷检测
不只会拍照的程序猿1 小时前
《嵌入式AI筑基笔记02:Python数据类型02,从C的“硬核”到Python的“包容”》
开发语言·笔记·python
qq_416018722 小时前
用户认证与授权:使用JWT保护你的API
jvm·数据库·python
王夏奇2 小时前
笔记-关于python复习
python
AC赳赳老秦2 小时前
OpenClaw 全平台安装详解:Windows 10/11、macOS、Linux 零踩坑指南 (附一键脚本)
大数据·人工智能·python·django·去中心化·ai-native·openclaw