%matplotlib inline not working

My students are working through an intro to jupyter tutorial. %matplotlib inline is at the start of the first cell, but plots are not being displayed until they also execute plt.show(). It is more than one student. I don’t know why this is happening. I remember having to specify interactive back in the day. Has that been resurected?

Thanks,

Ryan

Interesting. Sixteen days ago, there was this related report of much the same experience.

Unfortunately, there was no follow-up after that. As you’ll see, what was provided in the initial post didn’t offer enough details to really formulate how to proceed.

Based on your comment to the other thread, getting rid of %matplotlib inline fixes the problem.

The student is emailing me a small notebook that creates the problem now.

The notebook has this code across a few cells:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0,1,0.01)
y = np.sin(2np.pit)

plt.figure()
plt.plot(t,y)

And if you comment out the %matplotlib inline, everything is fine.

matplotlib version ‘3.10.5’.

Assume anaconda installed a few days ago. I can get other version information as needed.

Thanks,

Ryan

It isn’t matplotlib because related code (see below) works fine with 3.10.5 at the first link I suggested trying in the related thread. That session was running Python v3.12.8.


That code has syntax errors. I don’t is your local settings or not using Block code formatting?

I used:

%matplotlib inline
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)

However, that still seems overly not as simple as it can/should be these days. (Aside from the obvious that %matplotlib inline is the default and unnecessary these days.) Why the plt.figure() that has no settings and doesn’t get used. When you say, “but plots are not being displayed”. Does it always have that and is there maybe space it is trying to display that and blocking display of the unassigned, later plot?

Maybe please use GitHub gists to share?
Example previously-run notebook rendered via nbviewer. (Source notebook actually stored as Gihub gist , at which nbviewer was pointed.)
This may help also see if some output is in the notebook that you say isn’t displaying the expected plot?

Sorry, copying and pasting did something weird…. sorry about that.

Yes, the plt.figure() is not needed, but makes me feel nostalgic…..

But to answer the real question, if you have %matplotlib inline in the code, the output is a line of text about matplotlib lines. When you later execute plt.show(), it shows any plots that were created.

Does that make sense?

Ryan

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.

1 Like