avatarAmit Chauhan

Summary

The provided web content offers a comprehensive guide on using Matplotlib in Python for data visualization, covering its modules, components, and plotting examples, tailored for data scientists and machine learning engineers.

Abstract

The article "Step-by-Step Depth Introduction of Matplotlib with Python" serves as a detailed tutorial for individuals involved in data science and machine learning. It emphasizes the importance of visualization in data analytics and introduces Matplotlib as a powerful tool for creating graphs and plots. The tutorial explains the two interfaces of Matplotlib—object-oriented API and pyplot—and illustrates various components of plots such as axis, legends, tick labels, and titles. It provides practical examples, including basic line plots with one or two inputs, the use of markers, grid display, figure size adjustments, axis labeling, and text annotation within plots. The article aims to equip readers with the knowledge to effectively visualize data using Python, demonstrating how to install Matplotlib, manipulate plot elements, and enhance the readability of visualizations.

Opinions

  • The author suggests that Matplotlib's pyplot module is particularly useful for those familiar with MATLAB, as it offers a MATLAB-friendly interface.
  • The article conveys that the use of markers in plots is beneficial for differentiating multiple data sets within a single plot.
  • It is implied that the ability to customize figure sizes and grid displays is crucial for creating clear and presentable visualizations.
  • The author expresses the importance of labeling axes and adding text to plots to improve interpretability and provide context to the data presented.
  • The inclusion of recommended articles at the end of the content indicates the author's view that continuous learning and exploration of related topics are valuable for personal development in the field of data science and machine learning.

Step-by-Step Depth Introduction of Matplotlib with Python

A useful handful of examples for data science and machine learning projects

This library is frequently used by data scientists, python developers, and machine learning engineers to make graphs and plots for visual analysis.

Visualization has become an integral part of data analytics. The matplotlib has many modules depending upon the applications. Some of the modules are axis, pyplot, contour, dates, figure, etc.

As a data analysis with python, many learners or in production use pyplot for plots. The pyplot module of matplotlib is like a toolkit to make graphs and plots Matlab friendly.

To make a friendlier of making plots, we can use python.

To use the plyplot, we need to install the matplotlib library.

Pip install matplotlib

First, we need to know the two types of interfaces in the matplotlib i.e. object-oriented API and pyplot. For the API type, we need to use axes to render the plots with the figure method. The pyplot type is based on matlab i.e. state-based interface.

Let’s see the different components of the figures or plots

  • Axis: It is a vertical and horizontal scale used to generate ticks i.e. labels on the axis.
X and Y axis ticks and Label. A photo by Author
  • Legends: It is used inside of the plot area to name the line plot or other plot type.
Legend in the plots. A photo by Author
  • Major tick label: These are the values on the axis that are represented as a label.
Ticks in the plots. A photo by Author
  • Minor tick: These are the marks on the axis to represent the values.
Minor tick in the plot. A photo by Author
  • Title: It is used to describe the plot.
Title in the plot. A photo by Author

Let’s see some examples of plots with python

  • Basic line plot with matplotlib with one input
import matplotlib.pyplot as plt
plt.plot([10,11,12,13,14])
plt.show()
A simple line plot. A photo by Author

Observations:

  1. Importing the library with the alias name plt is used instead of matplotlib.
  2. The data generated the line in the graph.
  3. The plot methods have a parameter *args i.e. it can take multiple inputs. In the above example, we took a single input and it assumes this single input for the y-axis and generates the x-axis ticks based on the number of inputs from zero indexing.
Changes of X-axis ticks with number of inputs. A photo by Author
  • Basic line plot with matplotlib with two input
import matplotlib.pyplot as plt
plt.plot([10,11,12,13,14], [15,16,17,18,19])
plt.show()
Two input label values. A photo by Author

Now, in the above example, we give two inputs and this time the plot method takes these two as x and y input.

Observations:

  1. When we give two inputs, it takes them as a positional argument i.e. first input for the x-axis and the second input for the y-axis.

What if we don’t give the inputs to the plot method. The plot will empty with default axis values.

import matplotlib.pyplot as plt
plt.plot()
plt.show()
Empty plot. A photo by Author
  • Markers in plot method

They are very helpful to differentiate the multiple data in a single plot. We can change the shape of the data points with the help of markers.

Creating a circle of blue, red, green color.

import matplotlib.pyplot as plt
plt.plot([10,11,12,13,14], [15,16,17,18,19], 'bo')
plt.show()

The third argument is a marker and the change in the graph is shown below. In this parameter the first alphabet is a name of color i.e. ‘b’ means blue color and the second alphabet or character is the type of shape.

The blue round markers. A photo by Author

Let’s change data points with different markers. The dash line marker (b- -), the triangle marker (r^), and the square marker (rs).

Different markers in the plots. A photo by Author

Now, let’s plot the three data points in a single plot with different markers.

import numpy as np
# the samples are evenly distributed
t = np.arange(1., 20., 2)
# Data points with three different markers
plt.plot(t, t, 'r^', t, t**2, 'b--', t, t**3, 'gs')
plt.show()
Plots of different markers with different values. A photo by Author
  • To show the grid in the plot. Here, we need to specify the grid method with true to enable the grids in the plot.
import matplotlib.pyplot as plt
plt.plot([10,11,12,13,14], [15,16,17,18,19], 'rd')
plt.grid(True)
plt.show()
Grids in the plot. A photo by Author
  • There is also a magic to do changes in the figure size. If we put the code after the inputs then it will increase the size of the grid figure.
import matplotlib.pyplot as plt
plt.plot([10,11,12,13,14], [15,16,17,18,19], 'rd')
plt.figure(figsize=(15., 7.))
plt.grid(True, axis = 'y')
plt.show()
Different sizes of figure.. A photo by Author
  • If we put the figure method before the inputs then it will increase the figure size.
A photo by Author
  • The axis label depends on the inputs in the plot method. We can also manage to give axis labels with the axis method as shown below.
#Syntax
axis[xmin, xmax, ymin, ymax]
plt.plot([10,11,12,13,14], [15,16,17,18,19], 'rd')
plt.axis([10, 16, 15, 20])
plt.show()
Custom Axis values. A photo by Author
  • We can observe that the labels are changed after applying the axis method.

In this section, we will manage to give the labels to the axis.

plt.plot([10,11,12,13,14], [15,16,17,18,19], 'rd')
plt.axis([10, 16, 15, 20])
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.show()
Axis labels. A photo by Author
  • To add the text in the plot, we use the text method.
#Syntax
text(X-position, Y-position, r'text data')
plt.plot([10,11,12,13,14], [15,16,17,18,19], 'rd')
plt.axis([10, 16, 15, 20])
plt.xlabel('X-Label')
plt.ylabel('Y-Label')
plt.text(11.2, 15.9, r'This is second point')
plt.show()
Text in the plot. A photo by Author

I hope you like the article. Reach me on my LinkedIn and twitter.

Recommended Articles

1. 8 Active Learning Insights of Python Collection Module 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Pandas: Dealing with Categorical Data 5. Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Data Distribution using Numpy with Python 9. Decision Trees vs. Random Forests in Machine Learning 10. Standardization in Data Preprocessing with Python

Python
Programming
Data Science
Artificial Intelligence
Machine Learning
Recommended from ReadMedium
avatarKavishka Abeywardana
Principal Component Analysis (PCA)

8 min read