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

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

就不报错

相关推荐
用户8356290780512 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i2 小时前
python中类的基本结构、特殊属性于MRO理解
python
liwulin05062 小时前
【ESP32-CAM】HELLO WORLD
python
Doris_20233 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_20233 小时前
Python 模式匹配match case
前端·后端·python
这里有鱼汤3 小时前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员
大模型真好玩4 小时前
深入浅出LangGraph AI Agent智能体开发教程(五)—LangGraph 数据分析助手智能体项目实战
人工智能·python·mcp
测试老哥4 小时前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
百锦再4 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
张子夜 iiii5 小时前
4步OpenCV-----扫秒身份证号
人工智能·python·opencv·计算机视觉