Introduction to Matplotlib for Beginners

A simple guide on how to draw a plot with matplotlib.

Tirendaz AI
Star Gazers
4 min readFeb 25, 2021

--

Photo by Myriam Jessier on Unsplash

A plot is worth a thousand words. Data visualization is one of the important stages of data analysis. For example, you can draw a plot to find outliers. Matplotlib is the most used library for data visualization in Python. In this post, I’ll explain the following topics:

  • What is Matplotlib?
  • How to install Matplotlib?
  • Interactive and static plots
  • Saving the plot.

Before getting started, please don’t forget to subscribe to our youtube channel where I create content about ai, data science, machine learning, and deep learning.

Let’s dive in!

What is Maplotlib?

Before analyzing data, it is important to understand data. The simplest way to understand data is to draw a plot. Matplotlib is one of Python’s most important libraries used for data visualization.

Matplotlib Install

You can use different ways to install Matplotlib. The easiest way is to use platforms like Anaconda. If you use the Anaconda platform, important libraries such as Matplotlib, NumPy, Pandas pre-loaded. You can download Anaconda for free here. Another way to install Matplotlib is to use pip. You can use the following command on the command line.

Matplotlib and IPython developers worked together for interactive graphics and the Jupyter Notebook was created. Data scientists often use the jupyter notebook as it visualizes data well.

Before diving into examples, let’s import matplotlib and numpy.

Let’s create an array with Numpy.

Interactive Plots

You can set the %matplotlib notebook command to use an interactive window.

With this command, plots are displayed in a separate window. Let’s draw a simple line plot.

Since we only entered data, matplotlib automatically generated the x and y axes. Let’s add a title on the plot.

Let’s name the x-axis.

With every process we do, the plot is automatically modified without running the code over and over again.

Static Graphics

You can use the %matplotlib inline command to see the plots inline.

Let’s draw a plot of x values versus y values.

The plot function has a third argument after the x and y values. This argument indicates the linetype and color of the chart. These letters and symbols come from the MATLAB program. To show this, let me create data.

Let’s draw this data in different formats. I’m going to use three different lines on a plot.

Save the Graph

You can save the plot with the savefig method. For example, let’s save the plot in png format.

There are two important parameters here. You can use the dpi for the image quality. The bbox_inches parameter allows you to remove the white space around your plot.

Conclusion

As a result, it is very important to explore the data before analyzing data. Data visualization is one of the important steps in data science projects. In this blog post, I talked about matplotlib library. Matplotlib is one of Python’s most important libraries used for data science. You can find the notebook here.

Thank you for reading. I hope you enjoy it. Don’t forget to follow us on YouTube | GitHub | Twitter | Kaggle | LinkedIn 👍

If this post was helpful, please click the clap 👏 button below a few times to show me your support 👇

--

--