You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否本地运行Colab中的PyTorch+matplotlib代码?报错求助

问题解决:AttributeError: PathCollection.set() got an unexpected keyword argument 'lable'

这个报错是拼写错误导致的:在绘制测试数据的plt.scatter调用中,你把参数名label误写成了lable(缺少字母e),matplotlib无法识别这个不存在的参数,因此抛出错误。

修正后的完整代码

import matplotlib.pyplot as plt
import torch

weight = 0.7  # b in linear regression
bias = 0.3  # a in linear regression

start = 0
end = 1
step = 0.02
X = torch.arange(start, end, step).unsqueeze(dim=1)
y = weight * X + bias

train_split = int(0.8 * len(X))

X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]

def plot_predictions(train_data=X_train, train_labels=y_train,
                     test_data=X_test, test_labels=y_test,
                     predictions=None):
    # Plots training data, test data and compares predictions 
    plt.figure(figsize=(10, 7))

    # Plot training data in blue
    plt.scatter(train_data, train_labels, c="b", s=4, label="Training data")

    # Plot test data in green - 修正参数拼写错误
    plt.scatter(test_data, test_labels, c="g", s=4, label="Testing data")

    # Are there predictions?
    if predictions is not None:
        # Plot the predictions if they exist
        plt.scatter(test_data, predictions, c="r", label="Predictions")

    # Show the legend
    plt.legend(prop={"size": 14})


plot_predictions()

关键修改点

仅需将原代码中绘制测试数据那一行的lable改为label,matplotlib就能正确识别图例标签参数,正常执行代码并渲染图表。

内容的提问来源于stack exchange,提问作者Michal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:50:17