Matplotlib (Python Plotting Library)

Matplotlib Tutorial

Compared to textual data, human minds are more adaptable to the visual representation of data. Things are easier to comprehend when they are visualized. The graph is a better way to represent the data because it allows us to analyze it more effectively and make specific decisions based on data analysis. Understanding data visualization and the significance of data visualization is necessary prior to learning matplotlib.

Data Visualization

When it comes to presenting results, graphics provide an excellent method for exploring the data. The term "data visualization" is new. It conveys the idea that data representation goes beyond graphical representation (in place of textual representation).

Matplotlib Data Visualization

This can help classify patterns, corrupt data, outliers, and a lot more when learning about a dataset and discovering it. Data visualizations can be used to express and demonstrate key relationships in plots and charts with a little domain knowledge. In fact, the static focuses on data estimation and quantitative description. It gives you a useful set of tools for understanding qualitatively.

There are five key plots that are used for data visualization.

Matplotlib Data Visualization

The following five steps must be completed before the organization can decide:

Matplotlib Data Visualization
  • Visualize: By analyzing the raw data, complex data are made more user-friendly, understandable, and accessible. When a user needs to look up a specific measurement, a tabular data representation is used. A variety of charts are used to show patterns or relationships in the data for one or more variables.
  • Analysis: Cleaning, inspecting, transforming, and modeling data to extract useful information is what is meant by the term "data analysis." Whether it's a business or personal decision, we always base our decisions on our past experiences. Analyzing our past is all that will be done when making a specific decision. That may change in the future, so any business or organization can make better decisions with the right analysis.
  • Document Analysis: The process of organizing the useful data or information in a document in a standard format is known as document insight.
  • Change the Data Set: To make the decision more effectively, standard data is used.

Why need data visualization?

Why need Data Visualization

Data visualization can perform below tasks:

  • It identifies areas that need improvement and attention.
  • It clarifies the factors.
  • It helps to understand which product to place where.
  • Predict sales volumes.

Benefit of Data Visualization

Data visualization has a several advantages that help businesses and organizations make better decisions:

1. Developing methods for information absorption

Data visualization gives users access to a vast amount of information about business and operational conditions. Understanding the connections between multidimensional data sets aids decision-makers. Using maps, fever charts, and other rich graphical representations, it provides novel approaches to data analysis.

Visual data discovery is more likely to locate the information that an organization requires and, as a result, to result in a company that is more productive than its rivals.

2. Visualize relationship and patterns in Businesses

In today's highly competitive business environment, finding a correlation between operating conditions and business performance is essential, which is a key advantage of data visualization.

The executives are able to quickly identify the problem's root cause thanks to their ability to make such correlations.

Benefit of Data Visualization

Assume a food organization is looking their month to month client information, and the information is given bar graphs, which shows that the organization's score has dropped by five in the earlier months in that specific district; The data suggest that customer satisfaction is an issue in this area.

3. Accelerate your response to emerging trends Data visualization enables decision-makers to more effectively comprehend shifts in customer behavior and market conditions across multiple data sets.

An emerging opportunity for the company to act on new business opportunities before their competitor is revealed by having an idea about the sentiments of the customer and other data.

4. Geological based Visualization

It is a result of numerous websites offering web services and piqueing the interest of visitors. Websites of this kind are required to utilize location-specific data already contained in customer information.

The multi-platform data visualization library Matplotlib is a Python library based on the Numpy array. It is usable in shell, web applications, python scripts, and other graphical user interface toolkits.

In 2002, John D. Hunter came up with the idea for matplotlib. It is distributed under a license similar to BSD and has a thriving community of developers. The most recent version, 3.1.1, was released on July 1, 2019, and the first version was released in 2003.

Up until June 23, 2007, Python versions 2.7 to 3.6 are supported by Matplotlib 2.0.x. Matplotlib 1.2 marked the beginning of Python3 support. The most recent version of Matplotlib that supports Python 2.6 is 1.4.

Matplotlib's functionality can be enhanced with the help of a variety of toolkits. Some of these tools can be moved using the matplotlib source code, while others require external dependencies and must be downloaded separately.

  • Bashmap: It is a toolkit for plotting maps that includes coastlines, political boundaries, and various map projections.
  • Cartopy: It is a mapping library with arbitrary point, line, polygon, and image transformation capabilities in addition to definitions for object-oriented map projections.
  • Excel features: Utility programs can use Matplotlib to exchange data with Microsoft Excel.
  • Mplot3d: Plots in 3D are made with it.
  • Natgrid:: It is a Natgrid library interface for irregular gridding of spaced data.

Matplotlib Architecture

There are three different layers in the architecture of the matplotlib which are the following:

  • Backend Layer
  • Artist layer
  • Scripting layer

Backend layer

The various functions that are required for plotting are implemented in the backend layer, which is the figure's bottom layer. From the backend layer, there are three essential classes: FigureCanvas, which is the surface on which the figure will be drawn; Renderer, which is the class that handles drawing on the surface; and Event, which handles mouse and keyboard events.

Artist Layer

The architecture's second layer is the artist layer. It oversees the various plotting functions, such as the axis, which coordinates the renderer's use on the figure canvas.

Scripting layer

The topmost layer, the scripting layer, is where the majority of our code will run. The methods in the scripting layer take care of the other layers almost automatically; all we need to worry about is the current state (figure and subplot).

The Overarching Idea of Matplotlib a Matplotlib figure can be broken down into the following parts:

General Concept of Matplotlib

Concept of Matplotlib in General Figure:

  • It is a complete figure that could be holding multiple axes (plots). A Figure can be thought of as a canvas that holds plots.
  • Axes: A figure can have multiple axes. In the case of 3D models, it is made up of two or three Axis objects. There is a title, an x-label, and a y-label in each Axes.
  • Axis: Axises are the number of objects that look like lines and are in charge of creating the limits of the graph
  • Artist: Text objects, Line2D objects, and collection objects are all manifestations of an artist. The majority of artists use axes.

Installing Matplotlib

Before beginning any work with the plotting functions or Matplotlib itself, it must first be installed. The distribution that is installed on your computer affects how matplotlib is installed. The following are the methods for installing:

Utilize the Anaconda Python distribution Downloading the Anaconda Python distribution is the simplest method for installing Matplotlib. The Anaconda distribution already includes Matplotlib, so no additional installation is required.

  • Visit the official site of Anaconda and click on the Download Button
Installing Matplotlib
  • Choose download according to your Python interpreter configuration.
Installing Matplotlib

Install Matplotlib using with Anaconda Prompt

Matplotlib can be installed using with the Anaconda Prompt by typing command. To install matplotlib, open Anaconda Prompt and type the following command:


Installing Matplotlib

Install Matplotlib with pip

The python package manager pip is also used to install matplotlib. Open the command prompt window, and type the following command:

Verify the Installation

To verify that matplotlib is installed properly or not, type the following command includes calling.__version __ in the terminal.

Basic Example of plotting Graph

Here is the basic example of generating a simple graph; the program is following:

Output:

Basic Example of plotting Graph

It takes only three lines to plot a simple graph using the Python matplotlib. We can add titles, labels to our chart which are created by Python matplotlib library to make it more meaningful. The example is the following:

Output:

Basic Example of plotting Graph

The graph is more understandable from the previous graph.

Working with Pyplot

The collection of commands-style functions in matplotlib.pyplot replicate MATLAB-like functionality in matplotlib. Create a figure, create a plotting area in a figure, plot some lines in a plotting area, decorate the plot with labels, etc. are some of the things that can be done with the pyplot functions.

When we need to plot something quickly without creating any figures or axes, this is a useful tool.

Some states are stored across function calls when working with matplotlib.pyplot to keep track of things like the current figure and plotting area and to direct these plotting functions to the current axes.

The plot() function, which is frequently used to plot a graph, is provided by the pyplot module. Let's look at a straightforward illustration:

Output:

Working with Pyplot

The x-axis and y-axis of the graph are plotted using the program, and they range from 0 to 5. Matplotlib automatically generates the x values if we provide a single list to the plot() function, assuming that it is a sequence of y values. The default x vector has the same length as the y vector but starts at 0, as we know that the Python index starts at 0. As a result, [0, 1, 2, 3, 4,] are the x data.

We can pass the arbitrary number of arguments to the plot(). For example, to plot x versus y, we can do this following way:

Output:

Working with Pyplot

Formatting the style of the plot

The third argument is optional and is a format string that specifies the plot's color and line type. As can be seen in the graph that has been plotted above, the default format string is "b-," which is the solid blue. Consider the following illustration, in which the graph with the red circle is plotted.

Output:

Working with Pyplot

Example format String

'b'Using for the blue marker with default shape.
'ro'Red circle
'-g'Green solid line
'--'A dashed line with the default color
'^k:'Black triangle up markers connected by a dotted line

The matplotlib supports the following color abbreviation:

CharacterColor
'b' Blue
'g'Green
'r'Red
'c'Cyan
'm'Magenta
'y'Yellow
'k'Black
'w'White

Plotting with categorical variables

Matplotlib allows us to pass categorical variables directly to many plotting functions: consider the following example

Output:

Working with Pyplot

In the above program, we have plotted the categorical graph using the subplot() function. Let's a have a look on the subplot() function.

What is subplot()

To plot two or more plots in a single figure is how the Matplotlib subplot() function is defined. This technique can be used to separate two graphs that are plotted on the same axis. Matplotlib supports all subplots, including a 2x2 grid, 2x1 vertical, and 2x1 horizontal subplots.

The three arguments are accepted: Nrows, ncols, and index are these. It indicates the index, the number of rows, and the number of columns.

The subplot() function can be called in the following way:

Parameters:

  • *args:

The subplot's position is specified by a three-digit integer or three separate integers. On a grid with nrows row and ncol column, the subplot will take the index position if the three integers are nrows, ncols, and index in order.

The argument pos is a three-digit integer, with the number of rows as the first digit, the number of columns as the second digit, and the index of the subplot as the third digit. The subplot, for instance, is identical to the subplot (132).

Note: Passed integer must be less than 10.

  • **kwargs

The subplot() function also accepts the keyword arguments for the returned axes base class.

Consider the following example:

Creating different types of graph

1. Line graph

The line graph is one of charts which shows information as a series of the line. The graph is plotted by the plot() function. The line graph is simple to plot; let's consider the following example:

Output:

Creating different types of graph

We can customize the graph by importing the style module. The style module will be built into a matplotlib installation. It contains the various functions to make the plot more attractive. In the below program, we are using the style module:

Output:

Creating different types of graph

In Matplotlib, the figure (an instance of class plt.Figure) can be supposed of as a single container that consists of all the objects denoting axes, graphics, text, and labels.

Example-3

Output:

Creating different types of graph

The matplotlib provides the fill_between() function which is used to fill area around the lines based on the user defined logic.

Example-4

Output:

Creating different types of graph

2. Bar graphs

Bar graphs are one of the most common types of graphs and are used to show data associated with the categorical variables. Matplotlib provides a bar() to make bar graphs which accepts arguments such as: categorical variables, their value and color.

Output:

Creating different types of graph

Another function barh() is used to make horizontal bar graphs. It accepts xerr or yerr as arguments (in case of vertical graphs) to depict the variance in our data as follows:

Output:

Creating different types of graph

Let's have a look on the other example using the style() function:

Output:

Creating different types of graph

Similarly to vertical stack, the bar graph together by using the bottom argument and define the bar graph, which we want to stack below and its value.

Output:

Creating different types of graph

3. Pie Chart

A pie chart is a circular graph that is broken down in the segment or slices of pie. It is generally used to represent the percentage or proportional data where each slice of pie represents a particular category. Let's have a look at the below example:

Output:

Creating different types of graph

4. Histogram

Understanding the distinction between a bar graph and a histogram is the first step. A bar chart is used to compare various entities, while a histogram is used for the distribution. A histogram is a type of bar plot that compares the frequency of a set of values to a range of values.

For instance, we plot a histogram with respect to the bin using data from various age groups. Now, the range of values divided into a series of intervals is represented by the bin. Most bins are made of the same size.

Output:

Creating different types of graph

Let's consider the another example of plotting histogram:

Output:

Creating different types of graph

5. Scatter plot

When determining how much one variable affects another, scatter plots are typically used to compare variables. The data are presented as a series of points. The value of one variable, which determines the point's position along the horizontal and vertical axes, is assigned to each point. The value of the other variable, on the other hand, is assigned to each point.

Let's consider the following simple example:

Example-1:

Output:

Creating different types of graph

Example-2

Output:

Creating different types of graph

6. 3D graph plot

Matplotlib was initially developed with only two-dimension plot. Its 1.0 release was built with some of three-dimensional plotting utilities on top of two-dimension display, and the result is a convenient set of tools for 3D data visualization.

Three-dimension plots can be created by importing the mplot3d toolkit, include with the main Matplotlib installation:

When this module is imported in the program, three-dimension axes can be created by passing the keyword projection='3d' to any of the normal axes creation routines:

Let's see the simple 3D plot

Example-1:

Output:

Creating different types of graph

Example-2:

Output:

Creating different types of graph

Note: We can use the plot3D () to plot simple 3D line graph.

Example-3

Output:

Creating different types of graph

Important functions of Matplotlib

FunctionsDescription
plot(x-axis value, y-axis-values)It is used to plot a simple line graph with x-axis value against the y-axis values. show() It is used to display the graph.
title("string")It is used to set the title of the plotted graph as specified by the string.
xlabel("string")It is used to set the label for the x-axis as specified by the string.
ylabel("string")It is used to set the label for y-axis as specified by the string.
figure()It is used to control a figure level attributes.
subplots(nrows,ncol,index)It is used to add a subplot to recent figure.
subtitle("string")It adds a common title to the plotted graph specified by the string.
subplots(nrows,ncols,figsize)It provides the simple way to create subplot, in a single call and returns a tuple of a figure and number of axes.
set_title("string")It is an axes level method which is used to set the title of the subplots.
bar(categorical variables, values, color)It is used to create a vertical bar graph.
barh(categorical variables, values, color)It is used to create horizontal bar graphs.
legend(loc)It is used to make a legend of the graph.
xtricks(index, categorical variables)It is used to set or get the current tick locations labels of the x-axis.
pie(value, categorical variables)It is used to create a pie chart.
hist(value, number of bins)It is used to create a histogram.
xlim(start value, end value)It is used to set the limit of values of the x-axis.
ylim(start value, end value)It is used to set the limit of values of the y-axis.
scatter(x-axis values, y-axis values)It is used to plots a scatter plot with x-axis value against the y-axis values.
axes()It is used to add axes to the recent figure.
set_xlabel("string")It is an axes level method which is used to set the x-label of the plot specified as a string.
set_ylabel("string")It is used to set the y-label of the plot specified as a string.
scatter3D(x-axis values, y-axis values)It is used to plot a three-dimension scatter plot with x- axis value against the y-axis.
plot3D(x-axis values, y-axis values)It is used to plots a three-dimension line graph with x- axis values against y-axis values.

This tutorial covered the Python library matplotlib, a brief overview of data visualization, and the significance of data visualization in the organization's decision-making process. For the purpose of providing a visual representation of the data, we have plotted the various kinds of graphs.






Latest Courses