我有以下代碼試圖繪制圖形,但檢索錯誤串列索引必須是整數或切片,而不是串列
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]
s1mask=measured_2
predicted_2 = [72.50186,81.24528,76.507515,80.989363,81.19134,77.570047,81.816917,78.356714,82.2305032,None]
s2mask=predicted_2
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[s1mask], measured_2[s1mask], color='purple', marker='^',linestyle='dashed',label='Measured 2')
plt.plot(xs[s2mask], predicted_2[s2mask], color='purple', marker='*',linestyle='dashed',label='Predicted 2')
錯誤
plt.plot(xs[s1mask], measured_2[s1mask], color='purple', marker='^', markersize='2.5', linestyle='dashed',linewidth='0.5',label='Technique')
TypeError: list indices must be integers or slices, not list
uj5u.com熱心網友回復:
我不確定您在使用 的最后兩行嘗試做什么[s1mask],但是如果您洗掉它們,它將生成一個看起來正確的圖形。我還添加了一個圖例,因為我假設您想要標簽中的圖例。
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]
s1mask=measured_2
predicted_2 = [72.50186,81.24528,76.507515,80.989363,81.19134,77.570047,81.816917,78.356714,82.2305032,None]
s2mask=predicted_2
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, measured_2, color='purple', marker='^',linestyle='dashed',label='Measured 2')
plt.plot(xs, predicted_2, color='purple', marker='*',linestyle='dashed',label='Predicted 2')
plt.legend()
plt.show()
uj5u.com熱心網友回復:
相反,我認為xs應該是用于s1mask measured_2和的索引s2mask predicted_2:
[plt.plot(s1mask[i], measured_2[i],color='purple', marker='^',linestyle='dashed',label='Measured 2') for i in range(len(xs)-1)]
[plt.plot(s2mask[i], predicted_2[i],color='purple', marker='^',linestyle='dashed',label='Measured 2') for i in range(len(xs)-1)]

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368110.html
上一篇:根據特定元素將串列拆分為多個串列
