Sitemap
Kaushik K

Kaushik K

Python visualization using Matplotlib

A comprehensive guide for using Matplotlib for visualising

5 min readDec 26, 2022

--

Press enter or click to view image in full size

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

  1. Using pip:
pip install matplotlib

2. Using Conda:

conda install -c conda-forge matplotlib

3. Additionally, Through their official document, you may also try it online here.

Importing Matplotlib

Make your very first plot!

Press enter or click to view image in full size

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]

Press enter or click to view image in full size
Press enter or click to view image in full size

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

Press enter or click to view image in full size

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,

Press enter or click to view image in full size

Note: once we apply the style. Moving forwards, every plot will be impacted!

Styling Manually

We may also manually change the style.

Press enter or click to view image in full size

Plot Markers

To place the plot markers

Press enter or click to view image in full size

Chapter 4 - Plot labels and tiles

Giving titles

By using the loc= parameter, we can select where the title will appear (left, right, centre)

Press enter or click to view image in full size

Giving labels

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

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.

Press enter or click to view image in full size

Chapter 6 - Different types of graph

Bar Graph

Press enter or click to view image in full size

In this instance, the plots actually cross across. consequently, alter the width of the bar graph to make the plots visible.

Press enter or click to view image in full size

We can see that the plots do not overlap right now! and it is very evident.

To get horizontal bar graph,

Press enter or click to view image in full size

Scatter plot

Press enter or click to view image in full size

Pie chart

autopct is used to get the percentage

Press enter or click to view image in full size

Explode is a tool for separating values in pie charts.

Value 0 is No Explode

Value greater than 0 means Explode

Press enter or click to view image in full size

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.)

Press enter or click to view image in full size

--

--