Introduction

What is matplotlib?

Matplotlib is the most important library for creating data visualization in Python. It was originally developed by John D. Hunter in 2003. One of the key features of Matplotlib is its ability to produce a wide range of charts such as Line charts, bar charts, histograms, box plots and many other kind of plots. This versatility makes it a go-to tool for Data scientists, Engineers, and Analysts looking to visualize their data in Python.

Matplotlib integrates very well with many Data science libraries in Python such as Pandas, and Numpy making it an essential part of data analysis workflow in Python. Matplotlib provides lots of features and ways to create and modify charts in Python. Many data visualization libraries in Python are built on top of Matplotlib making it even more important to understand and get comfortable with it.

Why you should read this book?

Matplotlib Unlocked: Creating Beautiful Charts in Python is a comprehensive resource to learn and master the Matplotlib library in Python for Data Visualization and Insights. You will learn the tips and best practices of creating charts in matplotlib.

Whether you are taking your first step in data visualization or are already an advanced user looking for in-depth knowledge of matplotlib’s capabilities, this book has something for everyone.

What sets this book apart is its approach to teaching. Rather than merely walking you through the functions and features of matplotlib, it empowers you by:

  • Offering hands-on tutorial and exercises that teach you how to apply what you learned so you become more confident in using matplotlib for your data visualization tasks.
  • Instead of using fake or dummy data, you will learn from practical real-world data and apply those skills to find hidden insights.
  • You will learn to create charts incrementally. You will start from scratch and as you progress through more chapters and exercises, you will learn how to add more layers to your charts for complex visualization.
  • You will learn all the skills required to create, modify, and present data that have the most impact on your audience.

If you ever felt intimated by the matplotlib library then join me in this journey and I will show you how to master it and get all the praise you deserve for your work.

Happy visualizing! May your data tell compelling stories.

Prerequisites

This book assumes that you have some Python programming experience and that you are familiar with Python’s main data analysis and manipulation library Pandas.

If you don’t know Python and Pandas yet, there are many great websites where you can learn them and are completely free. keep an eye on our new website AdaCode.io where we will publish more tutorials to get you started with Python, Pandas and many other topics in Data Science and Programming in Python and other languages like javascript.

Installation

This book uses Python 3. please do not use Python 2, otherwise things not work as expected. There are various ways to install Python and Matplotlib but the easiest way is through Anaconda. We will use this for installtion.

Anaconda

Anaconda is a Python and R distribution for scientific computing which simplifies package installation and management. Anaconda comes with many libraries that Data scientists and Analysts need to do their work.

The process of installing Anaconda on Mac and Windows is almost same and since we will use a graphical user interface, installing it on either Mac or Windows is not that hard. If you encounter any problems then please search on Google (best way) or you can also email me at bholaprasad26@gmail.com

First, visit the Anaconda Website

Then click on the Download button.

Download Python
Figure 1. Download Python

This will download an executable file. Double click on it to start the installation process. Follow the instructions to install it as you do with any regular software on your machine.

Check installation

To verify that Python and Matplotlib get installed on your machine, run the following codes in Command prompt (windows) or Terminal (Mac or Linux). Don’t include the $ sign and the >>> sign.

1 $ python --version
2 'Python 3.11.5'
3 
4 $ python
5 >>> import matplotlib
6 >>> print(matplotlib.__version__)
7 3.8.0

Download code

The source code for this book’s examples is available at the following Github repository: matplotlib-python-book For those who are new to Git and Github, look for the download zip file from the <code> dropdown button at the top right-hand side. Those who are experienced with Git and Github can clone the repository from the command line. The instructions for cloning the project is included in the README.md file.

This repository also contains all the datasets used in this book. The data are in the data folder and the python notebooks are in the notebooks folder.

Jupyter notebook

Jupyter Notebook is an open-source web application that allows you to create and share documents that contains Python code, visualizations, Math equations, and texts. It is widely used in data science community for data cleaning and analysis.

Jupyter Notebook comes installed with Anaconda distribution so you don’t need to install it separately.

To launch the jupyter notebook run the following command in Terminal.

1 $ jupyter notebook

This command will start the jupyter Notebook server and open it in your default web browser. It will look something like this.

Jupyter Notebook
Figure 2. Jupyter Notebook

Click on the New dropdown menu at the top right corner and select notebook to create a new notebook.

To run a Python code type the following code in a notebook cell and hit the run button or press Shift + Enter on your keyboard.

1 print("Hello, World!)
Running Python code in Notebook
Figure 3. Running Python code in Notebook

To write text use the Markdown cell. Click on the Code dropdown menu at the toolbar and select Markdown or press ESC key and then m. This will change the code cell to a Markdown cell. To change a Markdown cell back to a code cell, use the dropdown menu or press ESC and then y.