In [1]:
import numpy as np

x = np.linspace(0, 10, 20)
y = x ** 2
In [2]:
# Let's plot the relationship between X and Y
# Don't hesitate to draw plot !
In [3]:
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
plt.plot(x, y)
Out[4]:
[<matplotlib.lines.Line2D at 0x1e401b4fe08>]
In [5]:
# OK, But there is no title and labels of x and y.
# Lets add them
In [6]:
plt.xlabel('X')
Out[6]:
Text(0.5, 0, 'X')
In [7]:
# Uhm... Something happened

# But's there exactly no plot at all except creating x label.
# We need to re-write plotting codes again.
In [8]:
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
Out[8]:
Text(0, 0.5, 'Y')
In [9]:
# Ah... Forgot to draw title of the plot
In [10]:
plt.plot(x, y)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Simple X and Y')
Out[10]:
Text(0.5, 1.0, 'Simple X and Y')
In [11]:
# We did that all we want draw. But this is a kind of stupid approach, isn't it?
# Let's do the same thing with object-oriented style
In [12]:
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.plot(x, y)
Out[12]:
[<matplotlib.lines.Line2D at 0x1e401e4d088>]
In [13]:
ax1.set_xlabel('X')
fig
Out[13]:
In [14]:
ax1.set_ylabel('Y')
ax1.set_title('Simple X and Y')
fig
Out[14]:
In [15]:
# Isn't it more intuitive? 
# In MATLAB style coding, it's hard to adjust previous plot.
# If you want to do that, you should re-write previous code again. That's bad.
# (Actually there is a way to catch previous handle, 
#  but it should use something special. ex) ax = plt.gca()