Thursday, April 24, 2014

Subplot in python using matplotlib

Here is an example of how to create subplot in python:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
data=np.arange(900).reshape((30,30))
for i in range(1,5):
    ax=fig.add_subplot(2,2,i)        
    ax.imshow(data)

plt.suptitle('Main title')
plt.show() 

Note that: the first two element in subplot(2,2,i) specify the number of subplot and the number of row and column. in this example it the plot has 2 rows and 2 columns. and i is the position of the subplot.
Also the title of the figure is defined by suptitle function .



Here is the result:

enter image description here




No comments:

Post a Comment