user1821176
user1821176

Reputation: 1191

Plotting from a created list

consider the code:

a = [1, 2, 3, 4]
for i in range(len(a)):

   for j in range(i+1, len(a)):
       dd21 = (a[i]-a[j])
       j = j + 1
       if i != j and dd21 !=0:
             print i, j, dd21
fig = plt.figure()
ax = fig.add_subplot(111)

ax.hist(dd21)
plt.tight_layout()

plt.tight_layout()
plt.show()

output = IndexError: invalid index to scalar variable.

What do I have to change my list for dd21 so that it can plot in a histogram?

Upvotes: 0

Views: 190

Answers (1)

Junuxx
Junuxx

Reputation: 14251

It seems it is your intention to store all the dd21 values you compute in the loop. However, currently you overwrite dd21 every time.

This should make it work, allowing you to plot the histogram:

a = [1, 2, 3, 4]
dd21 = [] # initialize empty list
for i in range(len(a)):

   for j in range(i+1, len(a)):
       dd21.append(a[i]-a[j])
       # ... continue as before

Upvotes: 1

Related Questions