能否本地运行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
相关产品推荐
相关产品推荐

