0

I am trying plot a graph with a numpy array but the error occur in plt.plot(s,s).

import numpy as np
import matplotlib.pyplot as plt

def npArrDefine():
    np.array=[]
    s=np.array
    for i in range(10):
        s.append(i+3)
    plt.plot(s,s)
    plt.axis([0,5,0,20])
    plt.show()
npArrDefine()
3
  • 3
    Maybe because you're doing this weird thing that you shouldn't s=np.array Commented Mar 23, 2018 at 18:45
  • Please edit in the full traceback for the error. Commented Mar 23, 2018 at 18:47
  • You are providing an alias to the np.array constructor and somehow expecting that it has an append method. That makes no sense. This also makes no sense: np.array = []. Commented Mar 23, 2018 at 18:48

1 Answer 1

2

There's a lot of things wrong with your code.

  1. np.array=[] and s=np.array. Here, you are setting a name that numpy uses to be an empty list (horrible!), and then you are setting s to be that empty list. Don't do this. Simply do s=[].

  2. Later on you are trying to plot by using plt.plot(s,s) which means you want to plot s against itself. This will always give you a straight 45 degree line with 0 intercept even if your code worked.

Your code block should be:

s=[]
for i in range(10):
    s.append(i+3)
s = np.array(s) #This line is optional, pyplot can use any array-like.
plt.plot(s)
...
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.