1

I have the following array in Jupyter, which I am using to plot as vertical lines from the x-axis.

array([6, 10, 18, 23, 29, 33, 50])

I am using pylab and matplotlib to do this.

I have produced the following code below thus far:

mylist = array([6, 10, 18, 23, 29, 33, 50])
mylist
for i in mylist:
  axvline(mylist[i])

However, I get the following error and only my last value plotted:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-106-37afa3c95407> in <module>
      2 mylist
      3 for i in mylist:
----> 4     axvline(mylist[i])

IndexError: index 10 is out of bounds for axis 0 with size 7

I am struggling to understand what to do to rectify the problem.

What error am I making here?

Photo showing graph being produced

I am unable to render my image in the question due to being a new user on this site.

1 Answer 1

2

You are using the indexing wrongly. When you do for i in mylist:, you are directly looping over the elements. So you can't access the elements then using mylist[i] but you need to directly use i

IndexError: index 10 is out of bounds for axis 0 with size 7

The error above occurs when you access the second element which is 10. When i=10, you use it as mylist[10] which throws error because the length of mylist is 7 because it has 7 elements.

Correct way:

for i in mylist:
    plt.axvline(i)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.