What Is Python Matplotlib?
[Link] is a plotting library used for 2D graphics in python programming
language. It can be used in python scripts, shell, web application servers and other graphical user
interface toolkits.
What is Matplotlib used for?
Matploitlib is a Python Library used for plotting, this python library provides and objected-
oriented APIs for integrating plots into applications.
Is Matplotlib Included in Python?
Matplotlib is not a part of the Standard Libraries which is installed by default when Python, there
are several toolkits which are available that extend python matplotlib functionality. Some of
them are separate downloads, others can be shipped with the matplotlib source code but have
external dependencies.
Python Matplotlib : Types of Plots
There are various plots which can be created using python matplotlib. Some of them are
listed below:
[Link] matplotlib problems
Source code:
1 from matplotlib import pyplot as plt
2
3 #Plotting to our canvas
4
5
[Link]([1,2,3],[4,5,1])
6
7 #Showing what we plotted
8
9 [Link]()
Output:
[Link] matplotlib problems
Source code:
1 from matplotlib import pyplot as plt
2 x = [5,2,7]
3 y = [2,16,4]
4 [Link](x,y)
5
6
[Link]('Info')
7 [Link]('Y axis')
8 [Link]('X axis')
9 [Link]()
Output:
[Link] matplotlib problems
Source code:
from matplotlib import pyplot as plt
1 from matplotlib import style
2 [Link]('ggplot')
3
4 x = [5,8,10]
5 y = [12,16,6]
6 x2 = [6,9,11]
7 y2 = [6,15,7]
8
9
[Link](x,y,'g',label='line one', linewidth=5)
10 [Link](x2,y2,'c',label='line two',linewidth=5)
11 [Link]('Epic Info')
12 [Link]('Y axis')
13
[Link]('X axis')
14
15 [Link]()
16 [Link](True,color='k')
[Link]()
Output:
[Link] matplotlib problems: Bar Graph
Source code:
from matplotlib import pyplot as plt
1
2 [Link]([0.25,1.25,2.25,3.25,4.25],[50,40,70,80,20],
3 label="BMW",width=.5)
4 [Link]([.75,1.75,2.75,3.75,4.75],[80,20,20,50,60],
5 label="Audi", color='r',width=.5)
6
7 [Link]()
8 [Link]('Days')
9 [Link]('Distance (kms)')
10 [Link]('Information')
11
[Link]()
[Link] matplotlib problems: Histogram
Source code:
import [Link] as plt
1 population_age = [22,55,62,45,21,22,34,42,42,4,2,102,95,85,55,110,120,
2 70,65,55,111,115,80,75,65,54,44,43,42,48]
3 bins = [0,10,20,30,40,50,60,70,80,90,100]
4
5
[Link](population_age, bins, histtype='bar', rwidth=0.8)
6 [Link]('age groups')
7 [Link]('Number of people')
8 [Link]('Histogram')
[Link]()
Output:
[Link] matplotlib problems: Scatter Plot
Source code:
1 import [Link] as plt
2 x = [1,1.5,2,2.5,3,3.5,3.6]
3 y = [7.5,8,8.5,9,9.5,10,10.5]
4 x1=[8,8.5,9,9.5,10,10.5,11]
5
6 y1=[3,3.5,3.7,4,4.5,5,5.2]
7 [Link](x,y, label='high income low saving',color='r')
8 [Link](x1,y1,label='low income high savings',color='b')
9 [Link]('saving*100')
10
11
[Link]('income*1000')
12 [Link]('Scatter Plot')
13 [Link]()
14 [Link]()
Output:
[Link] matplotlib problems: Area Plot
Source code:
1 import [Link] as plt
2 days = [1,2,3,4,5]
3 sleeping =[7,8,6,11,7]
4 eating = [2,3,4,3,2]
5
6
working =[7,8,7,2,2]
7 playing = [8,5,7,8,13]
8 [Link]([],[],color='m', label='Sleeping', linewidth=5)
9 [Link]([],[],color='c', label='Eating', linewidth=5)
10
11
[Link]([],[],color='r', label='Working', linewidth=5)
12 [Link]([],[],color='k', label='Playing', linewidth=5)
13 [Link](days, sleeping,eating,working,playing,
14 colors=['m','c','r','k'])
15 [Link]('x')
16
17 [Link]('y')
18 [Link]('Stack Plot')
19 [Link]()
20 [Link]()
Output:
[Link] matplotlib problems: Pie Chart
Source code:
1 import [Link] as plt
2 days = [1,2,3,4,5]
3
sleeping =[7,8,6,11,7]
4
5 eating = [2,3,4,3,2]
6 working =[7,8,7,2,2]
7 playing = [8,5,7,8,13]
8 slices = [7,2,2,13]
9
10 activities = ['sleeping','eating','working','playing']
11 cols = ['c','m','r','b']
12 [Link](slices,
13 labels=activities,
14
15
colors=cols,
16 startangle=90,
17 shadow= True,
18 explode=(0,0.1,0,0),
19
20
autopct='%1.1f%%')
21 [Link]('Pie Plot')
22 [Link]()
Output:
[Link] matplotlib problems : Working With Multiple Plots
Source code:
import numpy as np
1
2 import [Link] as plt
3 def f(t):
4 return [Link](-t) * [Link](2*[Link]*t)
5 t1 = [Link](0.0, 5.0, 0.1)
6
7
t2 = [Link](0.0, 5.0, 0.02)
8 [Link](221)
9 [Link](t1, f(t1), 'bo', t2, f(t2))
10 [Link](222)
11 [Link](t2, [Link](2*[Link]*t2))
12
[Link]()
Output:
[Link] matplotlib problems : Sine and Cosine graphs in the same plot
Source code:
# Importing required library
import numpy as np
import [Link] as plt
# Creating x axis with range and y axis
# Function for Plotting Sine and Cosine Graph
x = [Link](0, 5*[Link], 0.1)
y1 = [Link](x)
y2 = [Link](x)
# Plotting Sine Graph
[Link](x, y1, color='green')
[Link](x, y2, color='darkblue')
[Link]()
Output: