0% found this document useful (0 votes)
31 views

Matplotlib

Matplotlib is a Python library used for data visualization and plotting 2D graphs. It allows creating various types of plots like line plots, bar charts, scatter plots, histograms, and more. Matplotlib has tools for formatting plots, adding labels and titles, adjusting axis properties, and saving plots in different file formats. It is a popular choice for data visualization due to its simplicity, customization options, and ability to work with NumPy arrays and data frames.

Uploaded by

Srishti Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Matplotlib

Matplotlib is a Python library used for data visualization and plotting 2D graphs. It allows creating various types of plots like line plots, bar charts, scatter plots, histograms, and more. Matplotlib has tools for formatting plots, adding labels and titles, adjusting axis properties, and saving plots in different file formats. It is a popular choice for data visualization due to its simplicity, customization options, and ability to work with NumPy arrays and data frames.

Uploaded by

Srishti Arora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Matplotlib

Matplotlib is an amazing visualization library in Python for 2D plots of


arrays.
Data visualization which is the process of translating the numbers, text,
or large data sets into various types of graphs such as histograms, maps,
bar plots, pie charts, etc.
Matplotlib is a multi-platform data visualization library built on NumPy
arrays. It was introduced by John Hunter in the year 2002.
One of the greatest benefits of visualization is that it allows us visual access
to huge amounts of data in easily understandable visuals. Matplotlib
consists of several plots like line, bar, scatter, histogram etc.
Matplotlib is a python library used to create 2D graphs and plots by using
python scripts. It has a module named pyplot which makes things easy for
plotting by providing feature to control line styles, font properties, formatting
axes etc.
Plotting of data can be extensively made possible in an interactive way by
Matplotlib, which is a plotting library that can be demonstrated in Python
scripts. Plotting of graphs is a part of data visualization, and this property can
be achieved by making use of Matplotlib.

Introduction to Matplotlib

Matplotlib is the basic visualizing or plotting library of the python


programming language.

Matplotlib is able to create different types of visualization reports like


line plots, scatter plots, histograms, bar charts, pie charts, box plots, and
many more different plots. This library also supports 3-dimensional
plotting.

Installation :
Windows, Linux and macOS distributions have matplotlib and most of its
dependencies as wheel packages. Run the following command to
install matplotlib package :
python -mpip install -U matplotlib

Installation of Matplotlib in pycharm


To install Matplotlib by using the PIP command.

• Open File > Settings > Project from the PyCharm menu.
• Select your current project.
• Click the Python Interpreter tab within your project tab.
• Click the small + symbol to add a new library to the project.
• Now type in the library to be installed, in your
example "matplotlib" without quotes, and click Install Package.
• Wait for the installation to terminate and close all popup windows.

Now let’s check different categories of plots that Matplotlib provides.

• Line plot
• Histogram
• Bar Chart
• Scatter plot
• Pie charts
• Boxplot

Why is Matplotlib so Popular?

Matplotlib's popularity can be explained as follows:

• For beginners, it's simple and straightforward.


• It's open-source and free.
• Matplotlib is a highly customized and robust
• Matplotlib is good at working with data frames and arrays. Figures and
axes are treated as objects. It has a number of stateful plotting APIs. As a
result, methods like plot() can be used without any parameters.
• Those who have used Matlab or other graph plotting tools before will find
Matplotlib to be easy to use.
• Matplotlib can be used in a variety of contexts, such as Python scripts,
the Python and iPython shells, and Jupyter Notebooks.
• Matplotlib is a 2-D plotting library. However, there are several extensions
that can be used to produce complex visualizations such as 3-D graphs,
etc.
• It offers high-quality photos and plots in a variety of formats,
including png, pdf, and pgf.
• Controls numerous aspects of a figure, including DPI, figure color, and
figure size.
Most of the time we have to work with the Pyplot as an interface of Matplotlib.
So, we import Pyplot like this:

import matplotlib.pyplot

To make things easier, we can import it like this:

import matplotlib.pyplot as plt

Line Plots-
A line plot is used to see the relationship between the x and y-axis.

x, y are the coordinates of the horizontal and vertical axis

Matplotlib Plotting
Plotting x and y points

The plot() function is used to draw points (markers) in a diagram. By default,


the plot() function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

Example 1:

# Drawing a line from origin. (0,0) to (6,250)

# To draw a line

import matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)

plt.show()

Output:

Example 2:

# Draw a line from (5,100) to position (20,200)

import matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([5, 20])

ypoints = np.array([100,200])

plt.plot(xpoints, ypoints)

plt.show()

Output:
Example 3:
#Draw a line from points (10,20), (50,40), (20,60),(70,80)
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([10, 50, 20, 70])
ypoints = np.array([20, 40, 60, 80])
plt.plot(xpoints, ypoints)
plt.show()

Output:
Example 4:
#Making a line only with y-axis points
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([5,10,15,20,25])

plt.plot(ypoints, marker = 'o')


plt.show()

Output:

Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter 'o',
which means 'rings'.

Example:

import matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([100, 200])


ypoints = np.array([300, 400])

plt.plot(xpoints, ypoints, 'o')

plt.show()

Output:

Matplotlib Markers

You can use the keyword argument marker to emphasize each point with a
specified marker (vertex).

Example 1:
# Denoting markers with “+” symbol
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([5,10,15,20,25])

plt.plot(xpoints, marker='+')
plt.show()

Output:
Example 2:
# Denoting marker with “o” symbol
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([5,10,15,20,25])

plt.plot(xpoints, marker='o')
plt.show()

Output:
Example 4:

# shortcut string notation parameter to specify the marker. This parameter is


also called fmt, and is written with this syntax:

# marker|line|color
# Draw a line with marker “o”, dotted style “:” and red color “r”

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([10, 20, 30, 40])

plt.plot(ypoints, 'o:r')

plt.show()
Example 5:
# Increasing marker size
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([10, 20, 30, 40])
plt.plot(ypoints, 'o:r',ms=20)
plt.show()

Output:

Example 6:
# Marker face color as blue
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([10, 20, 30, 40])

plt.plot(ypoints, 'o:r',ms=20,mfc='b')
plt.show()
Output:

Marker reference

Marker

'o' Circle

'*' Star

'.' Point
',' Pixel

'x' X

'X' X (filled)

'+' Plus

'P' Plus (filled)

's' Square

'D' Diamond

'd' Diamond (thin)

'p' Pentagon

'H' Hexagon

'h' Hexagon

'v' Triangle Down

'^' Triangle Up
'<' Triangle Left

'>' Triangle Right

'1' Tri Down

'2' Tri Up

'3' Tri Left

'4' Tri Right

'|' Vline

'_' Hline

Line Reference

Line Syntax

'-' Solid line

':' Dotted line

'--' Dashed line


'-.' Dashed/dotted line

Color Reference

Color Syntax

'r' Red

'g' Green

'b' Blue

'c' Cyan

'm' Magenta

'y' Yellow

'k' Black

'w' White

Mentioning title and axis labels

A graph without title and labels is incomplete and one can not interpret
anything from that graph. We can include title and labels as follows.

Example:
import numpy as np

import matplotlib.pyplot as plt

x = np.array([25,30,15,63,25,57,17,83,52,45,37])

y = np.array([85,63,78,89,90,95,75,48,63,55,82])

plt.plot(x, y)

plt.title("Health Index")

plt.xlabel("Average Age")

plt.ylabel("Average Weight")

plt.show()

Output:
Bar plot

Mainly barplot is used to show the relationship between the numeric and
categoric values. In a bar chart, we have one axis representing a particular
category of the columns and another axis representing the values or count of
the specific category. Barcharts are plotted both vertically and horizontally and
are plotted using the following line of code:

plt.bar(x,height,width,bottom,align)

x: representing the coordinates of the x-axis

height: the height of the bars

width: width of the bars. Its default value is 0.8

bottom: It’s optional. It is a y-coordinate of the bar its default value is None

align: center, edge its default value is center

Example 1:

#vertical plot

from matplotlib import pyplot as plt

x = [5, 10, 15, 20, 25]

y = [25,50,75,80,85]

plt.bar(x,y)

plt.show()
Output:

Example 2:

#Horizontal Plot

from matplotlib import pyplot as plt

x = [5, 10, 15, 20, 25]

y = [25,50,75,80,85]

plt.barh(x,y)

plt.show()

Output:
Example 3:

# putting colors in bars

from matplotlib import pyplot as plt

x = [5, 10, 15, 20, 25]

y = [25,50,75,80,85]

plt.bar(x,y,color="green")

plt.show()
Output:

Example 4:
from matplotlib import pyplot as plt

x = [5, 10, 15, 20, 25]

y = [25,50,75,80,85]

plt.bar(x,y,width=2)

plt.show()

Output:
Matplotlib Adding Grid Lines

With Pyplot, you can use the grid() function to add grid lines to the plot.

Example:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([10,20,30,40,50,60])
y = np.array([40,50,60,70,80,90])

plt.title("Student Performance")
plt.xlabel("Average Age")
plt.ylabel("Average Marks")

plt.plot(x, y)

plt.grid()

plt.show()

Output:
Note:

plt.grid(axis = 'x') To display x axis grid only

plt.grid(axis = 'y') To display y axis grid only

Matplotlib Subplot

Display Multiple Plots

With the subplot() function you can draw multiple plots in one figure.

Example:

import matplotlib.pyplot as plt


import numpy as np

#plot 1:
x = np.array([2, 6, 8, 3])
y = np.array([3, 8, 6, 5])

plt.subplot(2, 1, 1)
# figure has 2 rows, 1 column, and this is figure number 1
plt.plot(x,y)

#plot 2:
x = np.array([3, 1, 11, 13])
y = np.array([7, 5, 13, 11])

plt.subplot(2, 1, 2)
# figure has 2 rows, 1 column, and this is figure number 2
plt.plot(x,y)

plt.show()

Output:

Matplotlib Scatter

Scatter plots are used to show the relationships between the variables and use
the dots for the plotting or it used to show the relationship between two
numeric variables.

The scatter() method in the Matplotlib library is used for plotting.

Creating Scatter Plots

With Pyplot, you can use the scatter() function to draw a scatter plot.

The scatter() function plots one dot for each observation. It needs two arrays of
the same length, one for the values of the x-axis, and one for values on the y-
axis:
Example:
import matplotlib.pyplot as plt

import numpy as np

x = np.array([5,7,8,7,2,7,2,9,4])

y = np.array([9,8,7,1,3,7,4,1,6])

plt.scatter(x, y)

plt.show()

Output:

Histogram

The most common graph for displaying frequency distributions is a


histogram.
A histogram is a graph showing frequency distributions. It is a graph showing
the number of observations within each given interval.

Example:
import matplotlib.pyplot as plt

import numpy as np

x = np.random.normal(100, 20, 50)

# value near 100 with daviation of 20, 50 values in total.

print(x)

plt.hist(x)

plt.show()

Output:

[ 96.24290908 120.53055445 116.79778842 87.04462603 125.20733193


120.9307256 116.76981623 101.84244669 90.68872475 76.9878571
107.08366654 111.99710358 109.85942953 79.05908541 74.98351492
120.01253531 86.16143602 84.43463687 79.66000995 78.81232201
110.99173771 91.35294313 74.69654658 126.45566875 94.1937674
111.81902877 84.407473 101.82221246 63.19600118 112.16132368
111.34565699 104.27843626 121.22216296 67.96262077 121.06161201
89.59123026 129.4856096 118.03250501 93.65062384 114.75340943
108.14144583 71.63329617 75.35953306 84.82386271 105.46096994
116.84794566 123.71053075 63.63145761 74.30650109 121.6748856 ]
Pie Charts

A pie chart (or circular chart ) is used to show the percentage of the whole.
Hence it is used when we want to compare the individual categories with the
whole.

Creating Pie Charts

Example 1:
import matplotlib.pyplot as plt
import numpy as np

y = np.array([75, 35, 20, 45])

plt.pie(y)
plt.show()
Output:
Example 2:
# adding labels to pie chart
import matplotlib.pyplot as plt
import numpy as np

y = np.array([75, 35, 20, 45])

mylabels = ["French", "Japnese", "English", "Spanish"]

plt.pie(y, labels = mylabels)


plt.pie(y)
plt.show()

Output:
Example 3:

#explode is used for wedges to stand out. Each value represents how far from
the center each wedge is displayed.

#English Language will stand out

import matplotlib.pyplot as plt

import numpy as np

y = np.array([10, 40, 35, 5])

mylabels = ["French", "Japenese", "English", "Spanish"]

myexplode = [0, 0, 0.2, 0]

plt.pie(y, labels = mylabels, explode = myexplode)

plt.show()

Output:
Example 4:

# specify colors for wedges

import matplotlib.pyplot as plt

import numpy as np

y = np.array([15, 35, 20, 45])

mylabels = ["French", "Japnese", "English", "Spanish"]

mycolors = ['green', 'yellow', 'blue', 'red']

plt.pie(y, labels = mylabels, colors=mycolors)

plt.pie(y)

plt.show()

Output:

You might also like