Python visualization using Matplotlib
A comprehensive guide for using Matplotlib for visualising
Prerequisites:
It is enough to have a fundamental knowledge of Python.
Covered Topics
Chapter 1 - Matplotlib
Chapter 2 - Basics of the plots
Chapter 3 - Styling the plot
Chapter 4 - Plot labels and titles
Chapter 5 - Ticks and Legend
Chapter 6 — Different types of graph
Chapter 7 - Sub-plots
Chapter 1 - Matplotlib
What is matplotlib?
Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. Matplotlib makes easy things easy and hard things possible.(Source Documentation)
Matplotlib Installation
Now, let’s install matplotlib on our machine.
The process of installing matplotlib on your computer is quite simple.
There are two approaches:
1. Using pip
2. Using conda
- Using pip:
pip install matplotlib2. Using Conda:
conda install -c conda-forge matplotlib3. Additionally, Through their official document, you may also try it online here.
Importing Matplotlib
Make your very first plot!
We receive an empty graph because X and Y have no data or because no data was provided.
Its using fake X and Y axes.
The X and Y expects something like a series/ list etc
The array input is accepted for y by default, while for x it accepts an index array of 0 to N-1.
Example :
For the above example,
X → [0, 1, 2, 3]
y → [2, 4, 6, 8]
Note: Everything we write will be visible since we are in the notebook! However, it could not be displayed if we are using another integrated development environment (IDE).
Hence we use, .show()
Chapter 2 - Basics of the plots
Figures
The graph shown above combines three separate plots into a single canvas or figure.
Axes: comprised of the x and y axis and can be 2D or 3D. Axes are made with anything that has labels, including the data that displays the border, etc.
In the above figure, these axes are gathered together.
To create the separate figure, we use .figure()
Resize the figure
Here, we may specify the figure’s size.
Get Kaushik K’s stories in your inbox
Join Medium for free to get updates from this writer.
Use of .figsize() is made. The measures given are in inches.
Chapter 3 - Styling the plot
Get all of the existing stylesheets
A style sheet is a document that instructs the IDE how to display a plot.
Here are some stylesheets
[
'Solarize_Light2',
'_classic_test_patch',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark',
'seaborn-dark-palette',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'tableau-colorblind10',
]Using the stylesheet
From the list above, select any style you like.
Now plotting again,
Note: once we apply the style. Moving forwards, every plot will be impacted!
Styling Manually
We may also manually change the style.
Plot Markers
To place the plot markers
Chapter 4 - Plot labels and tiles
Giving titles
By using the loc= parameter, we can select where the title will appear (left, right, centre)
Giving labels
Chapter 5 - Ticks and Legend
Ticks
On axes, data points are marked with ticks, which are also known as tick labels. The data points are automatically marked on the axes by matplotlib, but we can also establish our own axes with ticks and tick labels of our choosing.
Legend
Plot legends give meaning to a visualization, assigning meaning to the various plot elements.
You may see the legend by default on the upper left. It shows every label that was assigned to each plot. Nevertheless, we may adjust the position.
Chapter 6 - Different types of graph
Bar Graph
In this instance, the plots actually cross across. consequently, alter the width of the bar graph to make the plots visible.
We can see that the plots do not overlap right now! and it is very evident.
To get horizontal bar graph,
Scatter plot
Pie chart
autopct is used to get the percentage
Explode is a tool for separating values in pie charts.
Value 0 is No Explode
Value greater than 0 means Explode
Chapter 7 — Sub-plots
Creating a sub-plot
A subplot is a collection of smaller axes that can coexist in a single figure(Example: Perhaps you wish to layout three plots consecutively or something similar.)









