Context
I have to write a Jupyter notebook for a physics assignement, and I have to draw some graph showing the evolution of some quantities etc...
I have to explain almost each line of code I write by alternating between code and Markdown cells each time, including the plot() calls, which is where my problem comes from. In very simple terms, I have something like :
My code
We first import the matplotlib.pyplot module :
from matplotlib import pyplot as plt
Next, we creates lists of values from the data :
# making up stuff
x_values = range(10)
doubles = [x * 2 for x in x_values]
squares = [x**2 for x in x_values]
We first plot the curve for doubles :
%matplotlib inline
plt.plot(x_values, doubles, color='red', label='x * 2')
It shows the graph for the doubles (and only for the doubles)
Then we plot squares :
%matplotlib inline
plt.plot(x_values, squares, color='red', label='x^2')
It shows the graph for the squares (and only for the squares)
And, finally, we configure our graph and show it :
%matplotlib inline
plt.xlabel("x")
plt.ylabel("f(x)")
plt.ylim(0, 50)
plt.xlim(0, 11)
plt.legend()
plt.grid(True)
plt.show()
It shows an empty graph, with the labels, grid and limits set right, but no curve or legend at all
Expected
The last cells shows a graph with the two curves and the legend and all the stuff I configured
What happens/My problem
The last cells shows an empty graph with no curve or legend
What I've Tried
However, if I try to put the plot() calls in the last cell, with all the other stuff, I do get the expected graph, with the curves, legend, etc... But I cannot do that with my actual assignement, unfortunately :/
I also tried almost every value for the %matplotlib inline line, I tried multiple cells, multiple placements, etc... And it doesn't solve my problem
By googling a bit, it seems like the first line of a notebook behaves a bit differently in regards to matplotlib and all that, but this isn't nearly the first cell in my notebook, so it can't be that.
Honestly, I have no idea why that happens. I think I read somewhere that pyplot works as a sort of state-machine, so maybe the fact that it spreads over multiple lines is making it confused, but for other things (labels, style, etc...) it works as expected, so I'm clueless right now.
So I guess my question is : is this normal behaviour, is it a bug, or am I just missing something ?
EDIT: It seems like using fig, ax = plt.subplots(1, 1) and then plotting on ax works, but the problem is that it adds complexity that I cannot easily explain, that it is rather clunky, and that it doesn't answer "Why does it work when it's in the same cell, but not when the lines are in different cells ?"