Matplotlib
Matplotlib is a numerical mathematics extension NumPy and a great package to view or present
data in a pictorial or graphical format. It enables analysts and decision makers to see analytics
presented visually, so they can grasp difficult concepts or identify new patterns.
There are two broad ways of using pyplo.
Using Global Functions
Object Oriented approach
Using Global Functions
The most common and easy approach is by using global functions to build and display a global
figure using matplotlib as a global state machine. Let’s look at some of the most commonly used
charts.
plt.bar – creates a bar chart
plt.scatter – makes a scatter plot
plt.boxplot – makes a box and whisker plot
plt.hist – makes a histogram
plt.plot – creates a line plot
Example 01: Creating plot on variables
# simple bar and scatter plot
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(5) # assume there are 5 students
y = (20, 35, 30, 35, 27) # their test scores
plt.bar(x,y) # Bar plot
# need to close the figure using show() or close(), if not closed any follow
#up plot commands will use same figure.
plt.show() # Try commenting this an run
plt.scatter(x,y) # scatter plot
plt.show()
You can use the histogram, line graph, and boxplot directly on a dataframe. You can see that it’s
very quick and does not take much coding effort.
Example 02: Creating plot on dataframe
#Example 02: Creating plot on dataframe
import pandas as pd
df = pd.read_csv('Data/iris.csv')
df.hist()# Histogram
df.plot() # Line Graph
df.boxplot() # Box plot:
Note: Box Plot is used to show the shape of the distribution, its central value, and its variability. In a box
and whisker plot: the ends of the box are the upper and lower quartiles, so the box spans the
interquartile range. The median is marked by a vertical line inside the box