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

plt.plot报错list indices must be integers or slices, not list如何解决

错误原因
  • Python原生列表仅支持整数、切片作为索引,你代码中xs[s1mask]的写法将另一个列表作为索引传入,不符合原生列表的索引规则,该写法仅适用于NumPy数组。
  • 你当前将s1mask直接赋值为带None的数值列表,也不是合法的布尔掩码,你的需求应为过滤掉列表中None对应的无效数据点。
解决方案

提供两种常用实现方式:

方式1:使用NumPy数组实现掩码索引

将列表转换为NumPy数组后,即可使用布尔掩码完成无效值过滤:

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(3,2))
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

measured_1 = [86.317,86.317, 86.3175, 86.317, 86.317, 86.317,86.317, 86.317,86.317, 86.317]
predicted_1 = [88.404, 88.404,88.404, 88.404, 88.404, 88.404, 88.404,88.404, 88.404, 88.404]

measured_2 = [76.36715368,85.08431999,80.44446786,83.86890173,83.86890173,79.46104068,83.65406637,78.45798577,82.66697681,None]
predicted_2 = [72.50186,81.24528,76.507515,80.989363,81.19134,77.570047,81.816917,78.356714,82.2305032,None]

# 转换为NumPy数组,生成非空布尔掩码
xs_np = np.array(xs)
measured_2_np = np.array(measured_2, dtype=np.float64)
s1mask = ~np.isnan(measured_2_np)
predicted_2_np = np.array(predicted_2, dtype=np.float64)
s2mask = ~np.isnan(predicted_2_np)

plt.plot(xs, measured_1, color='orange', marker='^',linestyle='dashed',linewidth=0.5,label='Measured 1')
plt.plot(xs, predicted_1, color='orange', marker='*',linestyle='dashed', label='Predicted 1')

plt.plot(xs_np[s1mask], measured_2_np[s1mask], color='purple', marker='^',linestyle='dashed',label='Measured 2')
plt.plot(xs_np[s2mask], predicted_2_np[s2mask], color='purple', marker='*',linestyle='dashed',label='Predicted 2')
plt.legend()
plt.show()

方式2:使用列表推导式原生实现

不需要引入NumPy依赖,直接通过列表推导式过滤无效值:

import matplotlib.pyplot as plt

plt.figure(figsize=(3,2))
xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

measured_1 = [86.317,86.317, 86.3175, 86.317, 86.317, 86.317,86.317, 86.317,86.317, 86.317]
predicted_1 = [88.404, 88.404,88.404, 88.404, 88.404, 88.404, 88.404,88.404, 88.404, 88.404]

measured_2 = [76.36715368,85.08431999,80.44446786,83.86890173,83.86890173,79.46104068,83.65406637,78.45798577,82.66697681,None]
predicted_2 = [72.50186,81.24528,76.507515,80.989363,81.19134,77.570047,81.816917,78.356714,82.2305032,None]

# 过滤掉值为None的对应元素
filtered_xs_measured2 = [x for x, y in zip(xs, measured_2) if y is not None]
filtered_measured2 = [y for y in measured_2 if y is not None]
filtered_xs_predicted2 = [x for x, y in zip(xs, predicted_2) if y is not None]
filtered_predicted2 = [y for y in predicted_2 if y is not None]

plt.plot(xs, measured_1, color='orange', marker='^',linestyle='dashed',linewidth=0.5,label='Measured 1')
plt.plot(xs, predicted_1, color='orange', marker='*',linestyle='dashed', label='Predicted 1')

plt.plot(filtered_xs_measured2, filtered_measured2, color='purple', marker='^',linestyle='dashed',label='Measured 2')
plt.plot(filtered_xs_predicted2, filtered_predicted2, color='purple', marker='*',linestyle='dashed',label='Predicted 2')
plt.legend()
plt.show()

注意:原代码中linewidth参数传入了字符串'0.5',建议统一传入数值类型0.5,避免潜在的类型兼容问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 17:06:03