Matplotlib

This notebook provides a brief overview of the matplotlib plotting library in python which is popular for generating 2D plots. The matplotlib gallery has extensive examples which can be customized for your requirements.

The figure in matplotlib is the area where everything is drawn on. Figures comprise of Axes or subplots. Axes or subplots are the area within the Figure where actual plotting occurs. Figures can have multiple axes's or subplots. Axes have xaxis and yaxis.

Let us look at a simple plotting example.

In [1]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5 * np.pi, 0.1)
y = np.sin(x)
fig = plt.figure() 
ax = fig.add_subplot(111)
ax.plot(x, y, color='red')
ax.set(xlim=[0, 16.0], ylim=[-1.2, 1.2], title='An Example', ylabel='Sine Curve', xlabel='x values')
plt.show()

In the above example, you are creating a figure in the line fig = plt.figure().

In the line, ax = fig.add_subplot(111), you are adding a subplot. The 111 indicates number of rows, number of column, and the first subplot. It is not really needed in this case.

In line ax.plot(), you are generating the plot. You are setting the parameters of the plot in the line ax.set(). Note that you have control over all aspects of the plot, the x axis limits, y axis limits, ticks etc.

The plot will not be displayed until you run the plt.show() command. If you want to save the figure as a png file, use the savefig command. You can save it in other formats like pdf also.

In [2]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5 * np.pi, 0.1)
y = np.sin(x)
fig = plt.figure() 
ax = fig.add_subplot(111)
ax.plot(x, y, color='red')
ax.set(xlim=[0, 16.0], ylim=[-1.2, 1.2], title='An Example', ylabel='Sine Curve', xlabel='x values')
fig.savefig("trial.png", bbox_inches='tight')

You can change the figure size by providing a figsize = (width, height) argument where both width and height are in inches.

In [3]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5 * np.pi, 0.1)
y = np.sin(x)
fig = plt.figure(figsize=(10.0, 8.0)) 
ax = fig.add_subplot(111)
ax.plot(x, y, color='red')
ax.set(xlim=[0, 16.0], ylim=[-1.2, 1.2], title='An Example', ylabel='Sine Curve', xlabel='x values')
plt.show()

Let us look at an example where you have two subplots or axes in the same figure. Note that the scatter() command provides the scatter plot.

In [4]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5 * np.pi, 0.1)
y = np.sin(x)
x1 = np.arange(0, 10)
y1 = x1 + 5.0

fig = plt.figure(figsize=(16.0, 6.0)) 

ax1 = fig.add_subplot(121) #indicates 1 row, 2 columns, first subplot
ax1.plot(x, y, color='red')
ax1.set(xlim=[0, 16.0], ylim=[-1.2, 1.2], title='Plot 1', ylabel='Sine Curve', xlabel='x values')

ax2 = fig.add_subplot(122) #indicates 1 row, 2 columns, second subplot
ax2.scatter(x1, y1, color='green')
ax2.set(xlim=[0, 12.0], ylim=[0, 16], title='Plot 2', ylabel='y values', xlabel='x values')
plt.show()

If you want them plotted one below the other see the changes in the add_subplot line.

In [5]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5 * np.pi, 0.1)
y = np.sin(x)
x1 = np.arange(0, 10)
y1 = x1 + 5.0

fig = plt.figure(figsize=(8.0, 12.0)) 

ax1 = fig.add_subplot(211) #indicates 2 rows, 1 column, first subplot
ax1.plot(x, y, color='red')
ax1.set(xlim=[0, 16.0], ylim=[-1.2, 1.2], title='Plot 1', ylabel='Sine Curve', xlabel='x values')

ax2 = fig.add_subplot(212) #indicates 2 row, 1 columns, second subplot
ax2.scatter(x1, y1, color='green')
ax2.set(xlim=[0, 12.0], ylim=[0, 16], title='Plot 2', ylabel='y values', xlabel='x values')
plt.show()

If you want to plot a histogram using the hist() command.

In [6]:
import numpy as np
import matplotlib.pyplot as plt
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
z = np.random.randn(10000) # generates 100000 standard normal realizations
x = mu + sigma*z 
fig = plt.figure() 
ax = fig.add_subplot(111)
ax.hist(x, bins = 10, normed=1)
ax.set(title='Histogram', ylabel='Probability', xlabel='x values')
plt.show()

If you remove normed=1 you get the frequency instead of probability.

In [7]:
import numpy as np
import matplotlib.pyplot as plt
mu = 100 # mean of distribution
sigma = 15 # standard deviation of distribution
z = np.random.randn(10000) # generates 100000 standard normal realizations
x = mu + sigma*z 
fig = plt.figure() 
ax = fig.add_subplot(111)
ax.hist(x, bins = 10)
ax.set(title='Histogram', ylabel='Frequency', xlabel='x values')
plt.show()

The matplotlib gallery has lot of examples along with the source code. If you are looking for a specific type of plot, go through the gallery and pick one closest to yours. Use that as a basis to develop your plot.