0

In Jupyter notebook (Python) within Visual Studio Code, when I run the following, a Figure with an Axes is plotted/shown in the output of the cell.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

enter image description here

Instead, I expected only that the Figure and Axes objects would be saved to fig, ax. I tried adding a semicolon after subplots(), but that made no difference. Also, adding %matplotlib inline did not help.

Is this a behavior specific to notebooks within Visual Studio Code? How can I prevent the plot showing immediately in the output when calling subplots()?

Stephen Frost
  • 218
  • 4
  • 13
  • `%%capture` works to prevent plot output, but usually you see people calling subplots() without any plot output and without having to use %%capture explicitly. If I must use capture, that is fine. Your link *does* answer my question (thank you!). I only thought it is usually not necessary for subplots(), that usually you get no output anyway – Stephen Frost Dec 07 '22 at 17:52
  • Actually, executing `%matplotlib` within a cell suppressed the plot output for subplots(). This I have seen as more common in usage than %%capture, although I thought it was supposed to be no longer necessary to explicitly add %matplotlib for the latest version of IPython/Jupyter. – Stephen Frost Dec 07 '22 at 18:10

0 Answers0