DEV Community

Bernice Waweru
Bernice Waweru

Posted on • Updated on

Matplotlib Basics

Data Visualization is a powerful way of presenting data in a readily acceptable and understandable way.
Several tools are used for data visualization including Tableau, PowerBI, and D3.js. Additionally, Python offers several plotting and graphing libraries such as Plotly, Matplotlib and Seaborn.

In this article, we will focus on Matplotlib.

Matplotlib

Matplotlib is a plotting library used in creating interactive,static and animated visualizations.
The library is popular because:

  • it is fast,powerful and efficient
  • it is open source
  • it works well with various OS.

Usage

To use Matplotlib:

import matplotlib

The pyplot module is used for plotting therefore we import it as:

import matplotlib.pyplot as plt

Figure: The area that contains all the plots, titles and elements.
Axes: They delimit the chart area

Methods in Matplotlib

In this section we will look at some common methods used for plotting.

plot() and show()
Lets us examine the above code closely.
First we import Numpy to work with numpy arrays, and import pyplot for plotting.
The plot() function takes two parameters; the first specifies values in the x-axis(horizontal) and the second specifies values in the y-axis(vertical).
The plot() function draw points on the diagram and the default is a line from point to point.
The show() function displays all open figures.

Syntax: matplotlib.pyplot.show(*, block=None)

It starts an event loop that looks for all currently active figure objects, and opens one or more interactive windows that display the figures.

For a more descriptive figure we can include a title using the title() function.
To label the x axis we use xlabel() and ylabel() to label the y axis.

title(),xlabel(), ylabel()

So far we have a basic descriptive plot, but for more effective data visualization we can add some formatting to increase the appeal of our plots.
Let us explore some pyplot formatting.

  1. Consecutive Plots

You can plot consecutive plots as follows:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_csv("https://raw.githubusercontent.com/dphi-official/Assignment_Solutions/master/Standard%20Metropolitan%20Areas%20Data%20-%20train_data%20-%20data.csv")
plt.plot(data.physicians, data.income,color = 'cyan')
plt.show()
plt.plot(data.work_force, data.income,color = 'red')
plt.show()
Enter fullscreen mode Exit fullscreen mode

consecutive plots

However, we can improve this presentation to be more informative by plotting the two plots on the same figure. You can make better comparisons of different data points.

consecutive plots
The legend() function is used to add a legend that describes elements in the graph.
The plot() function takes additional parameters that enable use to include more description of how the graphs should appear.
We use color to specify the color, label to provide a label to be used by the legend() function.
Other parameters include marker to specify how markers should be displayed and the linestyle indicating the linestyle to be used in the plot.

linstyle and markers
You can achieve the same using:

linestyle and markers
The b represents the color, the 'o' is the marker and the -- is the linestyle.

Sometimes you might need to resize the figure. The figsize argument is used for resizing.
Syntax: plt.figure(figsize= (widthpixels, heightpixels))

Image description

2.Multiple Plots
We can also plot multiple plots in one figure using the subplot() function.
Syntax: plt.subplot(nrows,ncolumns,index)

The arguments specify the rows, columns, and index of the plot respectively.

Multiple plots

You're all set with the basics of matplotlib. We will explore matplotlib further in this post.

Top comments (0)