# How to plot a graph in python

Python provides a matplotlib library for data visualisation and graphing. Matplotlib,pyplot includes many easy-to-use functions which allow drawing different types of 2-D graphs.

## Introduction

Matplotlib is a popular open-source Python package that enables users to design graphs and plots on the given statistics and data. You can use other built-in functions and libraries such as numPy along with the library to plot datasets.

pyplot is a module of matplotlib that provides numerous plotting functions. Each pyplot function creates a figure based on the data provided. plot(), hist(), and bar provide different types of data visualisations. We will discuss each of the functions with an example later.

## Installation of matplotlib

You can download matplotlib from here and install the package manually.

Or you can just write the command in terminal:

pip install matplotlib

After installation, import Matplotlib.pyplot module with a name to use its built-in functions.

pyplot functions for different types of graphs.

## A simple plot

Creating a plot is very simple and quick with pyplot.

import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5])

plt.ylabel(‘Equation of a line’)

plt.show()

The code is self-explanatory. We have passed an array in the parameter of the plot function. Then, we gave a label to the y-axis and has displayed it. Notice that the snippet does not define the x axis, but the pyplot will automatically create according to y values.

## Line plot

The following lines of code will create a single line graph

import matplotlib.pyplot as plt

# setting x-axis and their corresponding y-axis values

x = [0, 2, 4]

y = [10, 15, 20]

# plotting the points

plt.plot(x, y)

# setting the label names for the axis

plt.xlabel(‘displacement’)

plt.ylabel(‘time’)

# giving a title to the graph

plt.title(‘Velocity line’)

# showing the figure

plt.show()

## Two lines in a plot

You can **create multiple lines plot**. The following lines of code two lines

`import`

`matplotlib.pyplot as plt`

`# setting x and y axis of the two lines`

x1 = [10,20,35]

y1 = [12,14,11]

x2 = [10,20,30]

y2 = [24,21,23]

# plotting the points and naming the linesâ€™

plt.plot(x1, y1, label = “line 1”)

plt.plot(x2, y2, label = “line 2”)

# labeling the x,y axis

plt.xlabel(‘x – axis’)

plt.ylabel(‘y – axis’)

# give title to the graph

plt.title(‘Two lines on the same graph!’)

# show a legend on the plot which will show the names of graph

plt.legend()

# function to show the plot

plt.show()

## Bar chart

The example creates a figure of a bar chart on the given dataset.

import matplotlib.pyplot as plt

# x-coordinates of left sides of bars

l = [1, 2, 3, 4, 5]

# heights of the bars

h = [20, 14, 30, 14, 28]

# labels for bars

lbl = [‘soap’, ‘shampoo’, ‘cream’, ‘cold drinks’, ‘ice-cream’]

# plotting a bar chart

plt.bar(l, h, tick_label = lbl,

width = 0.5, color = [‘blue’, ‘green’])

# labelling x and y axis

plt.xlabel(‘Items’)

plt.ylabel(‘total sold’)

# plot title

plt.title(‘Weekâ€™s sale’)

# function to show the plot

plt.show()

## Histogram

A histogram is another type of graph that has multiple axes.

numPy library has many built-in functions which provide support for different arrays and matrix operations. The Python built-in package also has a large collection of high-level mathematical operations for its array.

In the next example, we draw a histogram on the data of a numPy array.

# importing both packages

from matplotlib import pyplot as plt

import numpy as np

# defining numPy array

heights = np.array([20,75,51,30,16,23,48,40,9,29,69,8,88,76,55])

# sequence of the dataset

bins = [0,20,40,60,80,100]

# range of the plot with min and max values

range = (0,100)

# only first parameter is mandatory, others are optional and we can use to define other properties

plt.hist(heights, bins, range, color = ‘blue’, histtype = ‘bar’, rwidth = 0.7)

plt.title(‘Heights of pine trees’)

plt.show()

**Scatter plot**

**The Python built-in ****matplotlib library also allows you to create scatter plots. The following lines of code do the task**

import matplotlib.pyplot as plt

# x and y axes values

xAxis = [1,2,2,3,4,5,7,9,9,10]

yAxis = [2,3,5,6,7,8,9,4,12,13]

# plotting the points as a scatter plot

plt.scatter(xAxis, yAxis, label= “asterics”, color= “red”,

marker= “*”, s=25)

# x-axis label

plt.xlabel(‘length’)

plt.ylabel(‘frequency’)

# plot title

plt.title(‘New Scatter plot’)

# dispay the legend

plt.legend()

# show the scatter plot

plt.show()

**Pie chart**

import matplotlib.pyplot as plt

# naming labels

expenses = [‘Education’, ‘ration’, ‘bills’, ‘services’]

# portion covered

slices = [5, 4, 2, 6]

# defining colors for the labels

colors = [‘g’, ‘r’, ‘b’, ‘y’]

# plotting the pie chart

plt.pie(slices, labels = expenses, colors=colors,

startangle=50, shadow = False,

radius = 0.8)

# plotting legend

plt.legend()

# showing the chart

plt.show()

## Graph with Pandas library

You can also plot the data frame of the panda using matplotlib.

# importing the libraries

import pandas as pd

import matplotlib.pyplot as plt

# creating dataframe

df = pd.DataFrame({

‘Team’: [‘Brazil’, ‘Italy’, ‘Germany’],

‘Titles’: [5, 4, 4]

})

# plotting a bar graph

df.plot(x=”Team”, y=”Titles”, kind=”bar”)

plt.show()

## matplotlib has two types of interface

### 1. MATLAB style

All we have been discussed was Matlab style plotting, in which we focused on a single plot.

### 2. Object oriented interface

The python library also allows users to design a more sophisticated data visualisation using the OOP concept of objects and reusability.

**Fig and axes**

Figures are the container that holds plots called axes. A figure can contain multiple axes. Both are part of the matplotlib object-oriented interface.

The subplot is a function of pyplot which enables the user to control and manipulate all the plots created. The function is called with the pyplot instance. See the following example

The syntax is

*class_name, object_name = matplotlib.pyplot.subplots(â€˜no_of_rowsâ€™, â€˜no_of_columnsâ€™)*

import matplotlib.pyplot as plt

# defining x and y values

x =[0, 2, 3, 4, 6, 7, 8]

y =[0, 1, 2, 5, 8, 11, 15]

# creating the window with class ‘fig’

# and its â€˜axesâ€™ object. The parameters define rows and column

fig, ax = plt.subplots(1, 2)

# plotting graph for 1st column

ax[0].plot(x, y, ‘r–o’)

# plotting graph for second column

ax[1].plot(y, x, ‘b–o’)

# Gives a clean look to the graphs

fig.tight_layout()

plt.show()

## Conclusion

Python built-in open-source package matplotlib allows you to create figures and graphs on the given data. The dataset can be simple arrays, numPy arrays or panda data frames. pyplot module contains numerous functions that you can use to plot various scientific and statistical data visualisation.