In [3]:
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
In [4]:
# generate data
x = np.linspace(0,5,11)
y = x ** 2
In [5]:
x
Out[5]:
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
In [6]:
y
Out[6]:
array([ 0.  ,  0.25,  1.  ,  2.25,  4.  ,  6.25,  9.  , 12.25, 16.  ,
       20.25, 25.  ])
In [10]:
# Functional Method
plt.plot(x,y,)
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.title('sample plot')
Out[10]:
Text(0.5,1,'sample plot')
In [11]:
plt.subplot(1,2,1)
plt.plot(x,y,'r')

plt.subplot(1,2,2)
plt.plot(y,x,)
Out[11]:
[<matplotlib.lines.Line2D at 0x11df68940>]
In [16]:
# OO Method

fig = plt.figure()
axes = fig.add_axes([0.1,0.1,0.8,0.8])
axes.plot(x,y)
axes.set_xlabel('X label')
axes.set_ylabel('Y label')
axes.set_title('Plot Title')
Out[16]:
Text(0.5,1,'Plot Title')
In [40]:
fig = plt.figure()

axes1 = fig.add_axes([0.1,0.1,0.8,0.8])
axes2 = fig.add_axes([0.5,0.1,0.4,0.5])

axes1.plot(y, x)
axes2.plot(x, y,'r')
# axes title.
axes1.set_title('Large Plot')
axes2.set_title('small Plot')
Out[40]:
Text(0.5,1,'small Plot')

Matplotlib advance

In [54]:
fig, axes = plt.subplots(nrows=1,ncols=2)

axes[0].plot(x,y)
axes[0].set_title('First Plot')

axes[1].plot(y,x,'r')
axes[1].set_title('second plot')

plt.tight_layout()

Figure Size and DPI

In [61]:
fig,axes = plt.subplots(nrows=2,ncols=1,figsize=(5,2))

axes[0].plot(x,y)
axes[1].plot(y,x,'r')


plt.tight_layout()

SAVING A FIGURE

In [63]:
fig.savefig('matplot_fig.png',dpi=200)

Legends

In [70]:
fig = plt.figure()

ax = fig.add_axes([0.1,0.1,0.8,0.8])

ax.plot(x,y, label='X-Squared')
ax.plot(x*2,y, label='X-Doubled')

ax.legend(loc=0)
Out[70]:
<matplotlib.legend.Legend at 0x12159f710>

Linemarks | Types and Customizations

In [109]:
fig = plt.figure()

ax = fig.add_axes([0.0,0.0,0.8,0.8])

ax.plot(x,y,lw=2, alpha=0.5,ls='-.',
        marker='o', markersize=10,
     )
# # between markers
# ax.set_xlim([0,2])
# ax.set_ylim([0,5])
Out[109]:
[<matplotlib.lines.Line2D at 0x1230d9ac8>]