[Machine Learning Diary] Day 5— %matplotlib
When I read some kernel examples on Kaggle I always confused about what %matplotlib inline is. This is how I understand after read some documents.
Description:
Here is the code example:
# data analysis and wrangling
import pandas as pd
import numpy as np
import random as rnd
# visualization
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
The last line is to help Jupyter notebook to display result diractly after we call the plot function from matplotlib. This is one of the predefined magic function in IPython. You can treat it as
Suppose you do not add %matplotlib inline in your jupyter notebook you need to show result like this:
plt.plot(...)
plt.show()
This is equals to
%matplotlib inline
plt.plot(...)
Official Defination:
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
Conclution:
Please remember if you are not using Jupyter notebook, for example if you are running the program in Pycharm, please commend out %matplotlib inline. Otherwize there will be error in your program. How this can save you sometime.
Reference: