x and y must be the same size error python

Question:

that’s what i need to find where the error is (. Make a scatter plot of Nsteps versus starting number. You should adjust your marker symbol
and size such that you can discern patterns in the data, rather than just seeing a solid mass of
points.)

*** basically this is my code:***

#in[]

import numpy as np 
import matplotlib.pyplot as plt

def collatz(N):
   iteration=0 
   listNumbers=[N]

    while N != 1:
         iteration += 1
         if (N%2) == 0:
              N = N/2
         else:
              N= (N*3) + 1
   

     print(i,'number of interactions',iteration)
     print(listNumbers)
     return iteration

N= int(input(' enter a number:'))

iteration=collatz(N)

#out[]

 enter a number:7
10000 number of interactions 16
[7]

#in[]

s=np.array([])
for i in range(1,10001,1):
    K=collatz(i)
    s=np.append(s,K)
print(s)

#out
(too big to paste here but i put the first 10 lines)

1 number of interactions 0
[1]
2 number of interactions 1
[2]
3 number of interactions 7
[3]
4 number of interactions 2
[4]
5 number of interactions 5
[5]
6 number of interactions 8
[6]
7 number of interactions 16
[7]
8 number of interactions 3
[8]
9 number of interactions 19
[9]
10 number of interactions 6
[10]

#in[]

plt.figure() 
plt.scatter(range(1,100001,1),s) #gives me the x and y must be the same size error here plt.xlabel('number of interations')
 plt.ylabel('numbes') 
plt.ylim(0,355) 
plt.xlim(0,100000)
plt.show()

 #Make a scatter plot of Nsteps versus starting number. You should adjust your marker symbol
and size such that you can discern patterns in the data, rather than just seeing a solid mass of
points.
Asked By: sumi

||

Answers:

That error means that range(1,100001,1) is not the same length as s. Try

print(len(range(1,100001,1)))
print(len(s))

to confirm. You need to have the same amount of x-coordinates as you do y-coordinates. Make those the same size and it will work.

It’s unclear to me from your question/code what you’re actually trying to plot, but what I’ve just described is the root of the issue.

Answered By: lcleary

Try:

plt.scatter(np.arange(s.size), s)

This will set the range of x values depending on the actual size of your array.

Answered By: Awuthme