I have a basic function that does a function and I want to plot it out with Matplotlib but it doesn't work. It even prints out the correct list but still Matplotlib wont show it right
Here's my code:
And what it outputs:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
from matplotlib import pyplot as plt
number = 14
count = 0
dev_x = []
dev_y = []
plt.plot(dev_x, dev_y)
while number != 1:
if(number % 2 == 0):
_number = number / 2
number = _number
print(number)
dev_y.append(int(number))
dev_x.append(int(count))
count = count 1
else:
_number = number * 3 1
number = _number
print(number)
dev_y.append(int(number))
dev_x.append(int(count))
count = count 1
print(count)
else:
count = count 1
print(dev_x)
print(dev_y)
plt.show()
print("DONE")
print(number)
CodePudding user response:
you are trying to plot dev_x
and dev_y
before populating them. You need to call the plotting function after adding all coordinates
from matplotlib import pyplot as plt
number = 14
count = 0
dev_x = []
dev_y = []
while number != 1:
if(number % 2 == 0):
_number = number / 2
number = _number
print(number)
dev_y.append(int(number))
dev_x.append(int(count))
count = count 1
else:
_number = number * 3 1
number = _number
print(number)
dev_y.append(int(number))
dev_x.append(int(count))
count = count 1
print(count)
else:
count = count 1
print(dev_x)
print(dev_y)
plt.show()
print("DONE")
enter code here
## move this line here
plt.plot(dev_x, dev_y)
print(number)