Why doesn't '%matplotlib inline' work in python script?

Viewed 32447

The following does not work:

enter image description here

However this totally works in Jupiter Notebook.

enter image description here

If I simply comment it out, the graph doesn't show up. (Maybe it won't show up anyways)

import pandas as pd
import matplotlib

from numpy.random import randn
import numpy as np

import matplotlib.pyplot as plt

df = pd.read_csv('data/playgolf.csv', delimiter='|' )
print(df.head())

hs = df.hist(['Temperature','Humidity'], bins=5)
print(hs)
3 Answers

If you are using notebook and run my_file.py file as a module

Change the line "%matplotlib inline" to "get_ipython().run_line_magic('matplotlib', 'inline')". Then run my_file.py using this %run It should look like this:

In my_file.py:

get_ipython().run_line_magic('matplotlib', 'inline')

In notebook:

%run my_file.py

This run my_file.py in ipython, which help avoid the bug

NameError: name 'get_ipython' is not defined

Related