Home > Net >  Issue retrieving list indices must be integers or slices, not list
Issue retrieving list indices must be integers or slices, not list

Time:11-27

I have the following code trying to plot a graph but retrieving error list indices must be integers or slices, not list

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')

Error

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

CodePudding user response:

I'm not sure what you are trying to do in the last two line with the [s1mask], but if you remove them it will produce a graph that looks correct. I also added a legend since I am assuming that you wanted one from the labels.

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()

CodePudding user response:

 Instead, I think xs should be the indices used for s1mask measured_2 and 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)]

plot

  • Related