0

I'm following an example from a textbook covering Matplotlib. The following code saves a graph when run through command-line Python, but when ran through Jupyter it saves an empty image. Why is that happening?

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')
fig.savefig('image.png')
Jean Broc
  • 45
  • 1
  • 6
  • I don't think so - it doesn't involve explicitly creating a Figure object like I did. – Jean Broc Nov 04 '20 at 16:07
  • Based on your code, I also don't think this a duplicate question. However, I will not vote to reopen it because the problem is not reproducible with the code you provided. – Mr. T Nov 04 '20 at 17:19

1 Answers1

0

I've tried running the same code in Jupyter but without your first line:

#%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
fig = plt.figure()
plt.plot(x, np.sin(x), '-')
plt.plot(x, np.cos(x), '--')
fig.savefig(r'C:\Users\new_folder\image.png')

Make sure you specify a correct path for the output image. This way is working for me just fine and I get the .png image

MustardRecord
  • 305
  • 4
  • 14
  • It didn't change anything for me, the saved image is still blank. – Jean Broc Nov 04 '20 at 16:43
  • 1
    Maybe try more matplotlib build-in commands, as `%matplotlib` or `%matplotlib qt`, instead of `%matplotlib inline`. You can list the available matplotlib backends with `%matplotlib --list` and try some – MustardRecord Nov 05 '20 at 08:08
  • 1
    Check some documentation here: https://ipython.readthedocs.io/en/stable/interactive/magics.html – MustardRecord Nov 05 '20 at 08:10