Here's a fairly clean and simple solution, doing the same you did, but a bit more pythonic:
from random import randint
import matplotlib.pyplot as plt
# n can be any number of course
n = 10
# start a list of tuples with a first tuple of coordinates (x, y)
cs = [(randint(0, 15), randint(0, 15))]
# create n-1 more of them after it
for __ in range(n - 1):
# cs[-1] is the last tuple in the list, so cs[-1][0] is the x of that tuple
cs.append((randint(cs[-1][0], cs[-1][0] + 5), randint(cs[-1][1], cs[-1][1] + 5)))
# these are handy for plotting, see below for nicer solution
x_values = [x for x, __ in cs]
y_values = [y for __, y in cs]
# the limits of the plot will be set to 3 beyond the maximum values
plt.xlim(0, max(x_values)+3)
plt.ylim(0, max(y_values)+3)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x_values, y_values)
# add the marks
for x, y in cs:
plt.plot(x, y, '.r-')
plt.show()
Even nicer, if you ask me:
from random import randint
import matplotlib.pyplot as plt
n = 10
cs = [(randint(0, 15), randint(0, 15))]
for __ in range(n - 1):
# I changed this for readability, doesn't even need a comment
cx, cy = cs[-1]
cs.append((randint(cx, cx + 5), randint(cy, cy + 5)))
plt.xlim(0, max(x for x, __ in cs)+3)
plt.ylim(0, max(y for __, y in cs)+3)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
# this zips up all the tuples into a tuple of lists of x's and y's
# and then unpacks that tuple
plt.plot(*zip(*cs))
# add the marks
for x, y in cs:
plt.plot(x, y, '.r-')
plt.show()
I think this is nicer because it avoids creating copies of your data, keeping all the coordinates in a nice and simple list of pairs (tuples) of x and y. All the operations just reference that list cs
which means it could be a really long one, and your script would still work quickly and without wasting space (or introducing the opportunity to accidentally change one copy, but forgetting the other).
Ah, and to complete the answer to your question, you want to start with:
n = int(input('Any positive number:'))
If you want to see where the data ended up in your chart:
for i, (x, y) in enumerate(cs):
plt.annotate(f'{i}:{(x, y)}', xy=(x, y), xytext=(x + 1, y - .5))