What is Magic and %matplotlib inline

Screen Link: https://app.dataquest.io/m/146/guided-project%3A-visualizing-earnings-based-on-college-majors/1/introduction

import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

x = [1,2,3,4]
y = [4,7,4,20]

plt.bar(x,y)

plt.plot()

In the instructions for this guided project, it tells me to run the Jupyter magic %matplotlib inline. What is that for? I’ve looked it up and says something about making is display in the output. But I made the above bar plot as a test and ran the code with and without the magic command and I get the same output both times. So i’m not sure what exactly are we including this for if I return the same bar plot either way?

Thank you!

3 Likes

Hi @burnsdillion,

Try restarting the kernel like this:

Best,
Sahil

1 Like

@Sahil , I’m still a bit confused. What does it actually do? Does it make it where the plot stays outputted even after the kernel restart?

Thanks!

2 Likes

Hi @burnsdillion

Instruction:
Run the Jupyter magic %matplotlib inline so that plots are displayed inline.

If you don’t run that command, the plots won’t be displayed. Once, you run that command, the plot will continue to be displayed even if you remove the code. That is why I suggested you to restart the kernel. After restarting the kernel, when you run code, the plot won’t be displayed as the changes made by %matplotlib inline has been removed.

Best,
Sahil

5 Likes

@Sahil, got it! Thank you!!

1 Like