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

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

就不报错

相关推荐
FreakStudio23 分钟前
一文速通 Python 并行计算:教程总结
python·pycharm·嵌入式·面向对象·并行计算
群联云防护小杜27 分钟前
从一次 DDoS 的“死亡回放”看现代攻击链的进化
开发语言·python·linq
Ice__Cai32 分钟前
Flask 入门详解:从零开始构建 Web 应用
后端·python·flask·数据类型
霸敛33 分钟前
好家园房产中介网后台管理完整(python+flask+mysql)
开发语言·python·flask
HenryLin33 分钟前
SHAP值的核心概念
python
Darach36 分钟前
坐姿检测Python实现
人工智能·python
Ice__Cai37 分钟前
Flask 之 Request 对象详解:全面掌握请求数据处理
后端·python·flask·request·python web框架
hui函数37 分钟前
Flask高效数据库操作指南
数据库·python·flask
灵犀海棠38 分钟前
FLASK项目快速构建
后端·python·flask
灰阳阳1 小时前
接口自动化测试大全(python+pytest+allure)
python·pytest·接口自动化·allure