import matplotlib.pyplot as plt
11 Matplotlib
Matplotlib is a Python library for data visualization. It offers variety of functions to plot different types of graphs which can be customised to create high quality figures. The pyplot
function in this library is used for instantiating a matplotlib graph object. The plot
function draws a line plot given two sequences of equal length. The get the scatter plot use scatter
. Properties such as line-width, line-color, marker-type, etc. can be easily customized using the appropriate keyword arguments. The pyplot
object also has functions for decorating the plots with axis-labels, title, text, legend, etc. The show
function renders the plot.
= range(1,11)
x = [a**2 for a in x]
y #line plot
plt.plot(x,y) #scatter plot
plt.scatter(x,y) "Number")
plt.xlabel("Square")
plt.ylabel( plt.show()
The image below shows the different customizable elements in a matplotlib figure. These elements are accessible via different function available for the pyplot object or the axes object. The axes object is accessible through the gca
function for the pyplot object. As shown above the axes label can be changed using the xlabel
and ylabel
for the pyplot object.
Image source - https://matplotlib.org.
To change the x-axis ticks there is no direct way to do this using the pyplot object. So we need to create an axes object to modify x-ticks. The set_ticks
function available for the axes.xaxis
object can ge used to specify a list having locations for xticks.
= range(1,11)
x = [a**2 for a in x]
y #line plot
plt.plot(x,y) #scatter plot
plt.scatter(x,y) "Number")
plt.xlabel("Square")
plt.ylabel(= plt.gca()
ax range(1,11))
ax.xaxis.set_ticks( plt.show()
We can also have multiple plots in one graph i.e. different plots sharing the same axes.
= range(1,11)
x = [a**2 for a in x]
y = [a**3 for a in x]
z ="^", color="red")
plt.scatter(x,y,marker="*", color="purple")
plt.scatter(x,z,marker"Number", fontsize=12)
plt.xlabel("Value", fontsize=12)
plt.ylabel(=12)
plt.xticks(fontsize=12)
plt.yticks(fontsize"Squares","Cubes"])
plt.legend([ plt.show()
Matplotlib has a variety of in-built graph types that can be used to display information as required. Below are examples of some of the plots available in matplotlib. The title of these plots represent the corresponding function avaiable for the pyplot object.
Quiz: Write a program to make a graph as shown below.
Show answer
= [a**2 for a in x]
y =0.5)
plt.barh(x,y, height"Value")
plt.xlabel("Number")
plt.ylabel(range(1,11))
plt.yticks("Square"], loc="lower right")
plt.legend([ plt.show()
11.1 subplots
Many a time we need to plot multiple graphs in one figure for effecient visual analysis. In Matplotlib terminilogy these plots are refered to as subplots. The pyplot
class has subplots
function that return a figure and and axes object. These can be used to access and manipulated different elements of the graph. In addition, subplots can take as argument the total number of plots to create a figure. This function has a keyword argument figsize
to specify the size of the plot.
= range(1,11)
x = [a**2 for a in x]
y = [a**3 for a in x]
z
= plt.subplots(2,1,sharex=True) #two rows and one column
fig, ax
0].scatter(x,y,marker="^", color="red")
ax[1].scatter(x,z,marker="*", color="purple")
ax[1].set_xlabel("Number")
ax[0].set_ylabel("Squares")
ax[1].set_ylabel("Cubes")
ax[ plt.show()
The subplots in the above figure can be stacked horizontally by changing the subplots statement to plt.subplots(1,2)
.
Similarly, we can create different arrangements for subplots and save an image using savefig
function of the figure object. The resolution of the resulting image can be controlled using the dpi
argument. The alpha
argument in the code below is used to make the bars transparent.
= range(1,11)
x = [a**2 for a in x]
y = [a**3 for a in x]
z
= plt.subplots(2,2) #create subplots with two rows and two columns
fig, ax 0,0].scatter(x,y,marker="^", color="red")
ax[0,1].bar(x,y,color="red")
ax[
1,0].scatter(x,z,marker="*", color="purple")
ax[1,0].bar(x,z,color="purple",alpha=0.1)
ax[1,1].bar(x,z,color="purple")
ax[
"subplot.png", dpi=300)
fig.savefig( plt.show()
11.2 subplot (without ‘s’)
The subplot()
function is similar to subplots with a difference that it take an additional argument - index. This can be used to make axes span multiple columns within the subplot.
= range(1,11)
x = [a**2 for a in x]
y = [a**3 for a in x]
z
= plt.subplot(2,1,1)
ax1 = plt.subplot(2,2,3)
ax2 = plt.subplot(2,2,4)
ax3
="^", color="red")
ax1.scatter(x,y,marker="red",alpha=0.1)
ax1.bar(x,y, color="*", color="purple")
ax2.scatter(x,z,marker="purple")
ax3.bar(x,z,color
"subplots2.png",dpi=300)
plt.savefig( plt.show()