In [5]:
import matplotlib
import matplotlib.pyplot as plt

import numpy as np
import pandas as pd

In [6]:
matplotlib.__version__, np.__version__, pd.__version__


Out[6]:
('3.2.1', '1.18.2', '1.0.3')

2 Plots side by side


In [16]:
plt.clf()

# sample data
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# create figure and axes
fig, axes = plt.subplots(1,2)

ax1 = axes[0]
ax2 = axes[1]

# just plot things on each individual axes
ax1.scatter(x,y,c='red',marker='+')
ax2.bar(x,y)

plt.gcf().set_size_inches(10,5)
plt.show()


<Figure size 432x288 with 0 Axes>

2 plots one on top of the other


In [19]:
plt.clf()

# sample data
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# create figure and axes
fig, axes = plt.subplots(2,1)

ax1 = axes[0]
ax2 = axes[1]

# just plot things on each individual axes
ax1.scatter(x,y,c='red',marker='+')
ax2.bar(x,y)

plt.gcf().set_size_inches(5,5)
plt.show()


<Figure size 432x288 with 0 Axes>

4 plots in a grid


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

x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# plt.subplots returns an array of arrays. We can
# directly assign those to variables directly
# like this
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)

# just plot things on each individual axes
ax1.scatter(x,y,c='red',marker='+')
ax2.bar(x,y)
ax3.scatter(x,y,marker='x')
ax4.barh(x,y)

plt.gcf().set_size_inches(5,5)
plt.show()


Pandas plots


In [66]:
import matplotlib.pyplot as plt
import pandas as pd


df = pd.DataFrame({
    'string_col':['foo','bar','baz','quux'],
    'x':[10,20,30,40],
    'y':[1,2,3,4]
})

df


Out[66]:
string_col x y
0 foo 10 1
1 bar 20 2
2 baz 30 3
3 quux 40 4

In [69]:
plt.clf()
# plt.subplots returns an array of arrays. We can
# directly assign those to variables directly
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)

# bar plot for column 'x'
df.plot(y='x', kind='bar', ax=ax1)
ax1.set_xlabel('index')

# horizontal bar plot for column 'y'
df.plot(y='y', kind='bar', ax=ax2, color='orange')
ax2.set_xlabel('index')

# both columns in a scatter plot
df.plot('x','y', kind='scatter', ax=ax3)

# to have two lines, plot twice in the same axis
df.plot(y='x', kind='line', ax=ax4)
df.plot(y='y', kind='line', ax=ax4)
ax4.set_xlabel('index')

plt.subplots_adjust(wspace=0.3, hspace=0.5)

plt.show()


<Figure size 432x288 with 0 Axes>

Set subplot title


In [78]:
plt.clf()

# plt.subplots returns an array of arrays. We can
# directly assign those to variables directly
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)

# sample data
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# plot individual subplots
ax1.bar(x,y)
ax2.bar(x,y)
ax3.scatter(x,y)
ax4.plot(x)

ax4.set_title('This is Plot 4',size=14)

plt.subplots_adjust(wspace=0.3, hspace=0.5)

plt.show()


<Figure size 432x288 with 0 Axes>

Padding


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

# sample data
x = np.linspace(0.0,100,50)
y = np.random.uniform(low=0,high=10,size=50)

# plt.subplots returns an array of arrays. We can
# directly assign those to variables directly
fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)

# just plot things on each individual axes
ax1.scatter(x,y,c='red',marker='+')
ax2.bar(x,y)
ax3.scatter(x,y,marker='x')
ax4.barh(x,y)

# here, set the width and the height between the subplots
# the default value is 0.2 for each
plt.subplots_adjust(wspace=0.50, hspace=1.0)

plt.show()


Align axes


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

plt.clf()
# plt.subplots returns an array of arrays. We can
# directly assign those to variables directly
fig, ((ax1,ax2)) = plt.subplots(1,2)

np.random.seed(42)

x = np.linspace(0.0,100,50)

# sample data in different magnitudes
y1 = np.random.normal(loc=10, scale=2, size=10)
y2 = np.random.normal(loc=20, scale=2, size=10)

ax1.plot(y1)
ax2.plot(y2)

ax1.grid(True,alpha=0.3)
ax2.grid(True,alpha=0.3)

ax1.set_ylim(0,25)
ax2.set_ylim(0,25)

plt.subplots_adjust(wspace=0.3, hspace=0.5)

plt.show()


<Figure size 432x288 with 0 Axes>