Hi, Kristi W,
Normally, I think it's not as educational to just show someone the 'answer' (in this case, a program). But in this case, it's a simple program, and I can at least show you how I went about writing it, so that you might get more out of this answer than just 'the answer'.
I think it's a good idea, when you're writing code, to write in such a way that you can test what you've got, as you go along. So, although later, I'll show you the complete program, I didn't write it as a single piece. I started with just a Fibonacci sequence generator, called it with 10 (to get the first 10 Fibonacci numbers), and printed the result. This was just to (a) make sure my Python was correct (it wasn't, at first!), and then to verify that the sequence looked correct:
#!/usr/local/bin/python3
def FibonacciSequenceOfN(n):
sequence = []
if 1 <= n:
sequence.append(0)
if 2 <= n:
sequence.append(1)
for i in range(3, n):
sequence.append(sequence[-2] + sequence[-1])
return sequence
values = FibonacciSequenceOfN(10)
print(values)
Here's the output:
$ ./FibonacciGraphProgram
[0, 1, 1, 2, 3, 5, 8, 13, 21]
It looks correct, so next I added a new function to generate the ratios of each pair of values of the array given as its argument, which is the Fibonacci sequence created by the first function:
#!/usr/local/bin/python3
def FibonacciSequenceOfN(n):
sequence = []
if 1 <= n:
sequence.append(0)
if 2 <= n:
sequence.append(1)
for i in range(3, n):
sequence.append(sequence[-2] + sequence[-1])
return sequence
def RatioOfPairs(sequence):
ratios = []
last = -1
for this in sequence:
if last <= 0:
last = this
continue
ratios.append(last / this)
last = this
return ratios
values = RatioOfPairs(FibonacciSequenceOfN(10))
print(values)
Note that in this new 'ratio' function, I'm assuming that the sequence argument is an array (list), and that the values are ordered, from lower to higher values. I then skip the first values that are less than or equal to 0, so that I don't divide by 0. (The actual Fibonacci sequence starts with 0 and 1.) . If this were a 'production' program (i.e. something that others depended upon), then you'd probably want to actually sort the sequence before you use it, or make a comment stating that the function assumes that the sequence is ordered, low-to-high.
Anyway, here's the new output:
$ ./FibonacciGraphProgram
[1.0, 0.5, 0.6666666666666666, 0.6, 0.625, 0.6153846153846154, 0.6190476190476191]
Finally, we have what we want to plot, so I add the necessary import, and plot it:
#!/usr/local/bin/python3
# libraries
import matplotlib.pyplot as plt
def FibonacciSequenceOfN(n):
sequence = []
if 1 <= n:
sequence.append(0)
if 2 <= n:
sequence.append(1)
for i in range(3, n):
sequence.append(sequence[-2] + sequence[-1])
return sequence
def RatioOfPairs(sequence):
ratios = []
last = -1
for this in sequence:
if last <= 0:
last = this
continue
ratios.append(last / this)
last = this
return ratios
values = RatioOfPairs(FibonacciSequenceOfN(100))
print(values)
# use the plot function
plt.plot(values)
plt.show()
I cheated, a bit, since I'd forgotten how to do this. I did a web search for "python3 graph", and found this link: https://python-graph-gallery.com/. On that site, I went looking for line graphs, since I though that would be enough, and got to here: https://python-graph-gallery.com/line-chart/, then here: https://python-graph-gallery.com/120-line-chart-with-matplotlib/, and just copied the code. It wasn't until I'd run it that I was reminded that I needed to add the "plt.show()" call, but after that, everything seemed to be complete.
Unfortunately, I can't include the plotted output! :-(