Oh, you referencing the Python object information for the matplotlib lines, like how when I ran your code I saw this in addition to the plot:
[<matplotlib.lines.Line2D at 0x76f0c6169a90>]
That is the string representation of that object, showing its class name and memory address.
Importantly, that will show up if you run your code with or without %matplotlib inline
run in the notebook.
Strictly speaking that is supposed be there because that is what the last line plt.plot(t,y)
actually returns.
Recall that, by default, the last expression in a Jupyter code cell is special and what it returns or evaluates to gets displayed without you needing to do anything or add a print()
.
But it can be undesirable…
You can suppress it in this case by changing it so the last line ends with a ;
as I do in my code example I pointed you at there.
Then nothing is returned because the last expression is the semi-colon that returns nothing.
Implementing that in your would make the correct code
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0,1,0.01)
y = np.sin(2*np.pi*t)
plt.figure()
plt.plot(t,y);
See here or here, for more about this.
What I wrote above paraphrases this excellent comment by kynan at the first link I just referenced.
"Brilliant solution! The reason this works is because the notebook shows the return value of the last command. By adding ; the last command is “nothing” so there is no return value to show. "- comment by kynan here
Alternative Option to not show the object information….
Or as you mention in your first post, you can use plt.show()
as the last expression in the cell and then since the plot rendering object is the last thing returned, it is the only thing displayed in the output area.
However, you don’t strictly need plt.show()
these days because an open matplotlib figure encountered when the cell finishes will be displayed in modern Jupyter. So the semi-colon can be handy to suppress the object representation without needing to use plot.show()
at the end.