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

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 中,单项元组必须有一个尾随逗号,以将其与括号内的普通表达式区分开。

就不报错

相关推荐
码界奇点11 分钟前
Python与OpenCV集成海康威视工业相机从基础配置到高级应用的全方位指南
python·数码相机·opencv·相机·python3.11
来两个炸鸡腿13 分钟前
DW动手学大模型应用全栈开发 - (1)大模型应用开发应知必会
python·深度学习·学习·nlp
qq_4182478818 分钟前
恒源云/autodl与pycharm远程连接
ide·人工智能·python·神经网络·机器学习·pycharm·图论
我命由我1234523 分钟前
Python Flask 开发 - Flask 快速上手(Flask 最简单的案例、Flask 处理跨域、Flask 基础接口)
服务器·开发语言·后端·python·学习·flask·学习方法
大飞记Python24 分钟前
从零配置Python测试环境:详解路径、依赖与虚拟环境最佳实践
开发语言·python·环境配置·安装目录
资深设备全生命周期管理28 分钟前
SOP实时侦测系统
python
棒棒的皮皮30 分钟前
【OpenCV】Python图像处理几何变换之透视
图像处理·python·opencv·计算机视觉
小鸡吃米…39 分钟前
Python编程语言面试问题一
python·面试
天外飞雨44 分钟前
室内重跑EKF
python
五阿哥永琪1 小时前
Spring Boot 权限控制三件套:JWT 登录校验 + 拦截器 + AOP 角色注解实战
java·spring boot·python