MatPlotLib Syntax
MatPlotLib Syntax
Getting Started
Introduction
Matplotlib is a library for making 2D plots of arrays in Python. It makes heavy use of NumPy and other extension code to provide good performance even for large arrays.
Importing Module
The module can be imported in the following way
import matplotlib.pyplot as plt
Set-up
In order to use Matplotlib within Jupyter Notebooks and display plots inline with our code in each cell we need to run %matplotlib
magic code after we import the library.
Basically, a typical Jupyter Notebook in it’s first code cell will have:
%matplotlib inline
import matplotlib.pyplot as plt
Display Plots
plt.plot()
This function generates the plot and accepts a NumPy array or a list for the domain and range of the plot.
plt.plot([1, 2, 3], [1.1, 1.8, 3.3])
plt.show()
This function is more general and shows complex objects like figures with multiple subplots for example. To display the objects we just call the method without any inputs.
plt.plot([1, 2, 3], [1.1, 1.8, 3.3])
plt.show()
Dataframe Plots
There is a way to plot Pandas dataframe directly without creating any figures and plots explicitly.
dataframe_obj.plot(x='col_name01', y='col_name02, kind='scatter')
The code above will return an Axes object which is essentially a subplot that we can further customize.
Plot Labels & Title
Labels
To label the axes we can call plt.xlabel()
and plt.ylabel()
methods that both accept String as input.
Title
Labeling the plot with a title is just as straightforward by calling plt.title()
and passing a String.
Figure & Subplots
Figure
A figure acts as a container for all of our plots and has methods for customizing the appearance and behavior for the plots within that container.
fig = plt.figure()
Figure Dimensions
To tweak the dimensions of the plotting area, we need to use the figsize
parameter when we call plt.figure()
. This parameter takes in a tuple of floats:
fig = plt.figure(figsize=(width, height))
Where width
and height
are specified in inches.
Subplots
To add a new subplot to an existing figure, use Figure.add_subplot()
.
subplot_obj = fig.add_subplot(nrows, ncols, plot_number)
Lables
To add labels to subplots we can use:
subplot_obj.set_xlabel('x_label')
subplot_obj.set_ylabel('y_label')
Axis Limits
subplot_obj.set_xlim(n,m)
subplot_obj.set_ylim(n,m)
Example
Here’s an example of a figure having 2 subplots arranged in 2x1 format:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
plt.show()
Adding Data to Subplots
Subplots are Axis objects. To generate a line chart within an Axes object, we need to call Axes.plot()
and pass in the data.
subplot_obj.plot(x,y)
Axis objects will accept NumPy arrays and Pandas.Series objects.
Simple Subplot
We can create a quick and simple subplot the following way:
fig, ax = plt.subplots()
This will return Figure and Axes objects so that we can go straight to specifying subplot parameters that we need.
Overlaying Plots
Calling plt.plot()
multiple times will overlay charts onto one single plot. We can also differentiate each chart by specifying c='color_name'
.
Legend
Adding a legend is straight forward. We just need to specify the label
attribute with a String when calling the plt.plot()
function and then calling plt.legend()
and specifying the location with the loc
attribute.
plt.plot(x, y, c="red", label="line")
plt.legend(loc="upper right")
Bar & Scatter Plots
Bar Plots
Specifications
To plot bar charts we have to specify the following attributes:
- Position of the bars
- Width of the bars
- Position of the axis labels
There are $2$ methods to create a bar plot with Matplotlib. One is to call plt.bar()
and the other is to call Axes.bar()
. We will focus on the latter which has $2$ required parameters and $1$ optional: left
, height
that accept list like objects are the required and width
is the optional parameter:
# generate a single subplot by returning a Figure and Axes object
fig, ax = plt.subplots()
ax.bar(x_coordinates, bar_heights, width_of_bar)
Horizontal Bar Plot
To create a horizontal bar plot we call Axes.barh()
method and use the same input parameters as described above.
Label Alignment
Vertical Bars
- To change the position of ticks spanning the x-axis we can use
Axis.set_xticks()
. - Then we can specify tick labels by using
Axis.set_xticklabels()
. - We can also specify the orientation of labels by including a
rotation
parameter.
Both methods will take list like objects with String values as names.
Horizontal Bars
Axes.set_yticks()
Axes.set_yticklabels()
Scatter Plots
Specifications
To generate a scatter plot, we use Axes.scatter()
. It has 2 required parameters, x
and y
.
fig, ax = plt.subplots()
ax.scatter(x,y)
Histograms & Box Plots
Histograms
Specifications
To create a Histogram plot in Matplotlib we use the Axes.hist()
method.
subplot_obj.hist(list_of_values)
Bin Range
While only one parameter is required, an iteratable array of values, comparison of plots can become problematic since each can use different binning strategy. To avoid this we can specify range
of values by passing a tuple.
subplot_obj.hist(list_of_values, range=(n,m))
Number of Bins
We can also specify the number of bins to be plotted.
subplot_obj.hist(list_of_values, bins=k, range=(n,m))
Box Plots
Specifications
To generate a box plot we use Axes.boxplot()
method.
subplot_obj.boxplot(list_of_values)
Matplotlib will sort through the values, calculate the quartiles and generate the box and whisker diagram.
Multiple Box Plots
To create multiple box plot diagrams in one plot we can do the following:
subplot_obj.boxplot(dataframe_obj[col_names].values)
We can further use the same list col_names
with column name strings to specify x-axis tick labels.
subplot_obj.set_xticklabels(col_names, rotation=degree)
Plot Aesthetics
Hiding Tick Marks
In order to improve plots visually we should remove all unnecessary clutter. Lets start by hiding tick marks.
Axes.tick_params()
bottom=off
top=off
left=off
right=off
subplot_obj.tick_params(bottom=off, top=off, left=off, right=off)
Hiding Spines
Chart Spines are objects within the subplot Axes object and they need to be accessed separately to be turned on/off.
direction = 'left'
bool = False
subplot_obj.spines[direction].set_visible(bool)
The code above will turn off spines on the left. Similar logic follows for all other directions.
Colors & RGB
To specify color ranges with Matplotlib we need to scale every RGB value to the range $[0, 1]$ and represent it as a 3-tuple.
clr = (0/255,107/255,164/255)
Line Width
To set the width of the line on a chart we need to provide an additional parameter linewidth
when we call the Axes.plot()
method.
clr = (0/255,107/255,164/255)
chart_label = "Chart Label"
n = 5
subplot_obj.plot(x, y, label=chart_label, c=clr, linewidth=n)
Text & Annotating
To display text or annotate a chart we call the Axes.text()
method. Tis method has a few required parameters:
x
coordinate (float)y
coordinate (float)s
text (string)
subplot_obj.text(x,y, 'text_to_display')
Y-axis Labels
To set the labels on the y
axis of plots we can use ax.set_yticks(list_of_vals)
method
subplot_obj.set_yticks([0,33,66,100])
Horizontal Line
To add a horizontal line to a plot we can use Axes.axhline()
method with following parameters:
y
axis coordinate- RGB color in $[0,1]$
alpha
transparency
subplot_obj.axhline(50, c=(128/255,128/255,128/255), alpha=0.5)
Exporting to a File
Once plots are generated we would want to export it to an image file and save locally. This can be done using the method pyplot.savefig()
or Figure.savefig()
.
- Note that these have to be called before we display the figure using
pyplot.show()
.
fig.savefig('plot.png')