0% found this document useful (0 votes)
70 views

Data Visualisation With Matplotlib - by June Tao Ching - Sep, 2020 - Towards Data Science

This document summarizes key features of the Matplotlib library for data visualization in Python. It discusses basic charting functions like plt.plot(), plt.title(), and plt.xlabel(). It also covers annotating charts with text and arrows, highlighting regions, and customizing bar charts and tick labels. The goal is to help new users learn the basic features of Matplotlib in under 5 minutes.

Uploaded by

lalooprasad15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Data Visualisation With Matplotlib - by June Tao Ching - Sep, 2020 - Towards Data Science

This document summarizes key features of the Matplotlib library for data visualization in Python. It discusses basic charting functions like plt.plot(), plt.title(), and plt.xlabel(). It also covers annotating charts with text and arrows, highlighting regions, and customizing bar charts and tick labels. The goal is to help new users learn the basic features of Matplotlib in under 5 minutes.

Uploaded by

lalooprasad15
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

You have 1 free story left this month. Sign up and get an extra one for free.

Data Visualisation with Matplotlib


How to master the basic key features of Matplotlib in 5 mins

June Tao Ching Follow


Sep 19 · 4 min read

Photo by Isaac Smith on Unsplash

Matplotlib is one of the famous library to visualise data in Python. In this article, I will
go through these key functions that I think is useful and important so that those who are
new to Matplotlib can quickly pick it up.
https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 1/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

Basic Charting

Generate Mathematical Expression in title and label

Annotate your chart with text and arrows

Highlight your chart

Getting Started
As usual, we would need to install matplotlib package if we do not have it.

pip install matplotlib

Let’s import the package that we need in this tutorial.

Basic Charting

gure1.png

Let’s start with basic charting function in Matplotlib.

plt.title will create a title for your chart. It accepts normal string and Tex markup. If
you wish to use Tex markup to generate mathematical expression, you have to use $ to
enclose your text string. I prefer to use this because it looks way better than sin(x/2) !

https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 2/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

Here are some other examples of mathematical expression we can use in Matplotlib. You
may refer to the documentation for more examples.

plt.plot will generate a plot with the given data points, x and np.sin(x) are the data
points that we input at this case. By default, plt.plot will return a solid line style ( - ).
You may select other line style such as dashed line or dotted line.

Line Styles

We can customise the colour of the plot with color parameter. You can pass in colour
string or colour code. Same as title, you can pass in mathematical expression to label of
the plot too. plt.legend will create legend for the chart based on the attributes you have
passed in plot .

plt.grid will create a grid on a chart. Similar to the plot , you can define different line

style. alpha is the opacity of the grid.

plt.xlabel and plt.ylabel are used to label x-axis and y-axis.

plt.savefig is used if you want to save this chart as an image. If you never specify the
full path, the image will be saved in the same folder of your script by default.

https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 3/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

plt.show is used to display the chart when you are using terminal or script. It is
unnecessary in some IDE and Jupyter notebook.

Annotate Your Chart

gure2.png

I have used plt.figure(figsize=(10,10)) to enlarge the size of the chart so that we can
have a clearer look on the annotations. You may pass in the width and heigh to figsize

for your desired chart size.


https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 4/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

plt.annotate is used to annotate the intersection points of 2 lines. xy is the location of


annotation point while xytext is the location of the annotation text. I have demonstrate
3 types of arrow with different colour and size, you may refer to the documentation for
more options.

Highlight Your Chart

gure3.png

plt.axvspan is used to highlight along the x-axis while plt.axhspan is used to highlight
along the y-axis. We can define different colour with facecolor parameter and different
opacity with alpha .

Customise xtick in bar chart


Bar chart is also another commonly used chart in visualisation, let’s look into it too.

https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 5/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

gure4.png

plt.bar taking an array for your coordinate of the bar. If you wish to create a multiple
bar chart, you have to pass in the coordinate of both bars. x — width/2 and x + width/2

are passed into it to create a multiple bars chart.

plt.xtick is used to update the label to days ( ['Mon','Tue','Wed','Thu','Fri'] )


because it is [0,1,2,3,4] by default. We can add rotation to this label as well if the text
is too long. At this case, a 30 degree rotation is added to the tick.

Now you have learnt how to customise your charts with Matplotlib, I hope this article
will help you pick up the basic of Matplotlib and get familiar with other Matplotlib
functions with ease. Do drop me a comment if I made any mistakes or typos.

You can view the complete Jupyter notebook in my Github. Cheers!

. . .

If you enjoyed reading this piece, you might also enjoy these:

How to rewrite your SQL queries in Python with Pandas


Reproducing the same SQL queries result in Python

towardsdatascience.com

How to Master Python Command Line Arguments


A simple guide to create your own Python script with command line
arguments

towardsdatascience.com

https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 6/7
9/20/2020 Data Visualisation with Matplotlib | by June Tao Ching | Sep, 2020 | Towards Data Science

You can find links to my other works on Medium and follow me here. Thanks for
reading!

Sign up for The Daily Pick


By Towards Data Science
Hands-on real-world examples, research, tutorials, and cutting-edge techniques delivered Monday
to Thursday. Make learning your daily ritual. Take a look

Your email

Get this newsletter

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more
information about our privacy practices.

Data Data Science Data Visualization Python Matplotlib

About Help Legal

Get the Medium app

https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 7/7

You might also like