Matplotlib
Matplotlib is an amazing visualization library in Python
It helps us in interpreating the data in visual form of representation(eg. plotting a graph etc.)
Python Matplotlib : Types of Plots
There are various plots which can be created using python matplotlib. Some of them are
listed below:
Install Matplotlib with pip
The python package manager pip is also used to install matplotlib. Open the command
prompt window, and type the following command:
pip install matplotlib
In [1]: import matplotlib as mp
mp.__version__
Out[1]: '3.5.2'
Basic Example of plotting Graph
In [2]: #line graph
import [Link] as plt
# x-axis values
x = [5, 2, 9, 4, 7]
# Y-axis values
y = [10, 5, 8, 4, 5]
# Function to plot
[Link](x,y)
[Link](True)
# function to show the plot
[Link]()
In [3]: #Bar plot
import [Link] as plt
x = [5, 2, 9, 4, 7] # x-axis value
y = [10, 5, 8, 4, 2] # Y-axis value
[Link](x,y,label="English Books", color='r',width=.2) # Function to
[Link](True)
[Link]()
[Link]("Days")
[Link]("Nos. of books sold")
[Link]("Books sold in days")
[Link]() # function to show the plot
In [4]: import [Link] as plt
population_age = [22,55,62,45,21,22,34,42,42,4,2,80,75,55,55,44,43,42,48]
age_range = [0,10,20,30,40,50,60,70,80,90,100]
[Link](population_age, age_range, width=9) #histtype='bar'
[Link]('age groups')
[Link]('Number of people')
[Link]('Histogram')
[Link](True)
[Link]()
In [5]: import [Link] as plt
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0.1, 0.2, 0.1) # explode 1st slice
#[Link](sizes, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True
[Link](sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f
[Link]()
[Link]('equal')
[Link]()
In [6]: import numpy as np
import [Link] as plt
x = [1,1.5,2,2.5,3,3.5,3.6]
y = [7,8,9,10,12,15,17]
x1=[Link](0,10,1)
#x1=[Link](0,10,10)
y1=[17,15,12,10,9,7,4,3,2,0]
fig=[Link](figsize=(10,6),dpi=80)
[Link](x,y, label='high temperature high selling of AC',color='r')
[Link](x1,y1,label='low temperature high selling of heater',color='b')
[Link]('temperature-->')
[Link]('sellings-->')
[Link]('Scatter Plot')
[Link]()
[Link]()
print(fig)
Figure(800x480)
In [7]: import [Link] as plt
import numpy as np
import math #needed for definition of pi
x = [Link](0, [Link]*2, 0.1)
y1 = [Link](x)
y2 = [Link](x)
y3 = [Link](x)
[Link](figsize=(10,4),dpi=80)
[Link](1,3,1)
[Link](x,y1)
#[Link](x,y1) # for discrete plot
[Link]("angle")
[Link]("sine")
[Link]('sine wave')
[Link](1,3,2)
[Link](x,y2)
[Link]("angle")
[Link]("cosine")
[Link]('cosine wave')
[Link](1,3,3)
[Link](x,y2)
[Link]("angle")
[Link]("cosine")
[Link]('cosine wave')
[Link]()
In [8]: import [Link] as plt
import numpy as np
x=[Link](0,10,0.2)
y1=[Link](4*2*x)
y2=-1*[Link](4*2*x)
ax=[Link]()
[Link](x,y1,'b--')
[Link](x,y2,'r-*')
[Link]['left'].set_position(('data',0))
[Link]['bottom'].set_position(('data',0))
[Link]['top'].set_visible(False)
[Link]['right'].set_visible(False)
#[Link](0,color='r',linewidth=1)
[Link](-3,3)
[Link](-5,10)
[Link]()
Thank you
In [9]: from mpl_toolkits.mplot3d import axes3d
import [Link] as plt
import numpy as np
#if using a Jupyter notebook, include:
%matplotlib notebook
x = [Link](-5,5,0.1)
y = [Link](-5,5,0.1)
X,Y = [Link](x,y)
Z = X*[Link](-X**2 - Y**2)
fig = [Link](figsize=(6,4))
ax = fig.add_subplot(111, projection='3d')
# Plot a 3D surface
ax.plot_surface(X, Y, Z)
Figure 1
Out[9]: <mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x1a59df76e50>
In [10]: import numpy as np
import [Link] as plt
[Link]() # Create a new figure window
xlist = [Link](-2.0, 2.0, 100) # Create 1-D arrays for x,y dimensions
ylist = [Link](-2.0, 2.0, 100)
X,Y = [Link](xlist, ylist) # Create 2-D grid xlist,ylist values
F = X**2 + Y**2 - 1 # 'Circle Equation
[Link](X, Y, F, [0], colors = 'k', linestyles = 'solid')
[Link]()
Figure 2
In [ ]: