Data Visualisation With Matplotlib - by June Tao Ching - Sep, 2020 - Towards Data Science
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.
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
Getting Started
As usual, we would need to install matplotlib package if we do not have it.
Basic Charting
gure1.png
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
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.
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
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 .
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
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.
. . .
If you enjoyed reading this piece, you might also enjoy these:
towardsdatascience.com
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!
Your email
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.
https://towardsdatascience.com/data-visualisation-with-matplotlib-13aaf4787b30 7/7