Introducing Matplotlib - The Gramps of Python Visualization


Matplotlib is a Python-based plotting library with full support for 2D and limited support for 3D graphics which has application across diverse domains. More about can be found at:
Install matplotlib from the command prompt as :
  • conda install matplotlib
  • pip install matplotlib
Verifying the version details:
2

What can it do?

Import matplotlib's 'pyplot' as plt (this could be anything, plt is just a reference) and use the 'inline' to display the graph without using show() method (specific to Jupyter Notebooks)


1

Lets plot a simple graph
3.PNG
 
Description:
The x-axis has numbers ranging from [0-4] and the y-axis has the corresponding square values. plot() is the method used to draw the graphs and it has a variety of attributes ()line style, line width, marker type, marker size etc) which will be discussed in a while.
Lets make things a bit more complicated by plotting the graphs manually rather than letting matplotlib have all the control and fun
.
5.PNG
Description:
We create an empty canvas, then define the axes to be drawn (0.1,0.1,0.8,0.8 - represent the distance from left, bottom of the screen and the width , height of the figure respectively).  The difference between the previous plot and this one is:
  • In graph 1: plt.plot() took care of creating the canvas and the axes automatically
  • In graph 2: total control is in our hands
Now why would we wan't to choose the hard method to draw graphs when matplotlib can do it for us?

Now this is one crazy graph. isn't it. It involves subplots and customization (line colors) on each subplot.
Or if you are one crazy engineer who loves to make hings look super complicated, may be draw a graph with in another graph? To pull off such a maneuver, we will definitely want to have absolute control on the graph generation process. 
23

Lets see how the  inner plots & subplots are drawn and then we'll discuss about the crazy graph shown above in a while.
Lets draw a little more complicated inner plot:
7.PNG
Description:
Now we're looking at the  advantages of using the complicated approach of creating graphs (Object Oriented Approach to be more accurate) which enables us to define multiple axes and customize them per our requirement. To get a better hold of the figure, play around by changing the arguments inside the add_axes().
A notable difference : plt.xlabel() vs axes.xlabel()
Lazy way of drawing subplots
6.PNG
Description:
subplot() is used to draw multiple graphs with one canvas. It takes in as arguments, number of rows, number of columns and the plot number. So subplot(121) - for lazy bums or subplot(1,2,1) mean the same - I want a grid with one row and two columns and currently am drawing the first graph. Here the axes management is taken care by matplotlib automatically.
Note:
tightlayout() is used to beautify the graphs.
Object Oriented Approach to draw subplots - Thats how the programmers prefer the coffee to be brewed
Lets draw a grid to hold 4 subplots (2 rows and 2 columns)
8

Now to fill up the subplots with some colorful data
11.PNG


Description:

The axes is an array holding 4 axes objects. We can iterate through the array to perform various manipulations.
10.PNG
The objective is to make use of array indexing to draw graph in each subplot. For instance axes[0][0] would mean 'Row 1 and Column 1' and so on.
Now lets make the graphs looks beautiful:
What good is a graph without the ever so informative 'Legends'
12.PNG

13.PNG

Description:
We used a couple of tricks here:
figsize() to create a custom gird with width=15 and height=8
label to associate a string to be displayed as legend
legend() to include legend in the graphs
legend() takes in a few parameters to place the legend information in the graph. They include: best, upper right, upper left, lower left, lower right, right, center left, center right, lower center, upper center, center. We can even use loc=1,2..
So far we've been using markers (o,+,-- etc), color or c (r or red, b or blue and so on.The various other optinos we could use are:
linestyle or ls: Defines the way lines look. It could be : - or -- or -. or : or steps
another attribute related to line styling is: linewidth or lw
color or c: This can be a short notation like 'b' for 'blue' or the complete name itself. It accepts hex values as well for instance, #000000 for black
marker: Takes on values like: '+' or 'o' or '*' or 's' or ',' or '.' or '1' and so on
some more marker related attributes :
markerwidth, markersize, markeredgecolor, markerfacecolor

Lets plot a graph which has these attributes.

14.PNG

 

The range of axes can be limited per requirement using:
axes.axis('tight') - For tight fit
axes.set_xlim(<<takes a list as input>>) or axes.set_ylim(<<takes a list as input>>) - For a custom fit
18.PNG

Now for the DEMON plot we saw earlier with 9 grids, though it looks insanely difficult, if you've followed the documentation discussed so far, you should be able to draw it with ease. Lets plot the graph.
Note:
I am reading the data from an EXCEL sheet into PANDAS DATAFRAMES which is used to plot the 3*3 grid.

4.PNG
So we read a file 'book.xslx' containing the size of my database captured everyday for years [2009-2017] into a Pandas dataframe 'df'
The TS_SIZE column has database size for each data of the year which will be later aggregated as monthly database size for easy viewing.
Lets make the FETCH_DATE as the index for our convenience and find the sum of the database size for the whole month.
20.PNG
Now comes an interesting part. We need to fetch the SLICES of FETCH_DATE column providing the range of dates. The variables y2009 and so on will be used later to plot graphs in the individual subplots.
21.PNG
Lets use the above sliced data to plot graphs in the 3*3 grid.
22.PNG

Description:
As usual, create an empty canvas and a grid containing 9 subplots, 3 in each row with a figsize as shown. Using the array slicing mechanism discussed earlier, lets create 9 AxesSubplot objects to hold out input data for each year in [2009 - 2017]. tick_params() is one more beautification method used to change the way x,y ticks behave. We've removed the ticks in the graph for now. And thats it, here you have the crazy looking 3*3 grid.
23.PNG
In the next post, we ll see how piecharts, histograms, bar charts, scatter plots etc can be drawn using matplotlib.
ciao for now...

Comments