Home > other >  Convert 2 python lists into 1 Numpy Array with the format ((list1, list2))
Convert 2 python lists into 1 Numpy Array with the format ((list1, list2))

Time:11-07

Code:

import matplotlib.pyplot as plt
import numpy as np

poslist = []
numlist = []
i = 1

num = int(input('Number to check?'))
plt.title(str(num)   ': Collatz Graph')
plt.xlabel('Step')
plt.ylabel('Value')
print(num)
poslist.append(i)
numlist.append(num)

while True:
    i = i   1
    if num % 2 == 0:
        num = num // 2
        print(num)
    elif num == 1:
        break
    else:
        num = num * 3
        num = num   1
        print(num)
        poslist.append(i)
        numlist.append(num)

axislist = np.array((poslist, numlist))
print(axislist)
plt.plot(axislist)
plt.show()

I am trying to turn 2 lists into one Numpy Array. poslist, the first list, will add a number with each step that increments by 1 from the previous. (e.g [1, 2, 3, 4]). The second list, numlist, will append a number each step that corresponds with the graph when input is 100

CodePudding user response:

Do plt.plot(*axislist).

By default, plt.plot parses a single argument as y-coordinates. Hence, in your case each column is used to define a line. You need to pass both x coordinates and y coordinates, which you can do by adding *. In Python, * will split the numpy array into two subarrays defined by its rows.

  • Related