2

When using jupyter notebook, you have to just import matplotlib.pyplot as plt to create a plot. Why do we need %matplotlib inline?

import matplotlib.pyplot as plt
price = [100, 250, 380, 500, 700]
number = [1, 2, 3, 4, 5]
plt.plot(price, number)
plt.title("price / number")
plt.xlabel("price")
plt.ylabel("number")
plt.show()

[postscript] Why is there no difference in the following images? enter image description here enter image description here

  • 1
    Did you read e.g. https://stackoverflow.com/q/43027980/3001761? – jonrsharpe Jun 07 '20 at 17:11
  • 1
    I understand the need for "%matplotlib", but the fact is that I was able to plot with only "import matplotlib". Why? –  Jun 07 '20 at 17:16

2 Answers2

0

Thanks to @Georgy, fixed the problem. Using matplotlib.get_backend(), you can see the backend within the notebook is already set to inline by default.module:// ipykernel.pylab.backend_inline and It turns out that it's working on backend.

-1

that line allows you to plot graph in the same line. i.e in jupyter notebook below your running code. If you do not use that line, then the graph will be opened as a separate window.

Remove it and run the program it will be much clearer to what I am saying

Shubh Patni
  • 468
  • 1
  • 4
  • 7
  • strange. I have never used `%matplotlib inline` in jupyter notebook and all my plots are rendered inline - not in a separate window. I guess the OP is confused because of this - I wonder what gives! – finlytics-hub Jun 07 '20 at 17:23
  • If you compare the images above, you'll see that there is no difference –  Jun 07 '20 at 17:52