1. Sanjivani Rural Education Society's
Sanjivani College of Engineering, Kopargaon 423603.
-Department of Strucutral Engineering-
By
Mr. Sumit S. Kolapkar (Assistant Professor)
Mail Id- kolapkarsumitst@sanjivani.org.in
2. Ø Why data visualization in Python-
• Is a quick and easy way to convey the concepts in a
universal manner.
Ø What is data visualization-
• Is a graphical way of representing information and
data.
Ø Types of data visualization in Python-
• Plotting Libraries-
• Matplotlib-
• Pandas Visualization-
• Seaborn-
• ggplot-
• Plotly-
3. Ø What is Matplotlib-
• Is a plotting library for Python and it is numerical
mathematical extension of Numpy
• Is 2D and 3D plotting Python library
• It was introduced by John Hunter in the year 2002
Ø Matplotlib graphs-
4. Ø Importing Matplotlib in Python-
• import matplotlib.pyplot as plt
OR
• from matplotlib import pyplot as plt
Example-
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[6,7,8,9,10]
plt.plot(x,y)
plt.show ()
Example-
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[6,7,8,9,10]
plt.bar(x,y)
plt.show ()
5. Ø Importing Matplotlib in Python-
Example-
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[6,7,8,9,10]
Z=['b','g','r','black','pink']
plt.bar(x,y,color=Z)
plt.show ()
Base Color-
7. Ø Matplotlib Bar Plot-
• import matplotlib.pyplot as plt
x = ['Python','C','C++', 'Java']
y = [90,65,82,85]
plt.bar(x,y)
plt.show ()
8. Ø Matplotlib Bar Plot-
• import matplotlib.pyplot as plt
x=[ ]
y=[ ]
z=[ ]
plt.bar(x,y, width=0.4, color=“y”, align= “edge” (or center),
edgecolor=“r”, linewidth=10, linestyle=“:”, alpha=0.4,
label=“Popularity”)
plt.bar(x,z, width=0.4, color=“g”, align=edge, edgecolor=“r”,
linewidth=10, linestyle=“:”, alpha=0.4, label=“Popularity1”)...for
multiple bar graphs
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
9. Ø Matplotlib Bar Plot-
• import matplotlib.pyplot as plt
x=[ ]
y=[ ]
z=[ ]
plt.bar(x,y, width=0.4, color=“y”,label=“Popularity”)
plt.bar(x,z, width=0.4, color=“g”, label=“Popularity1”)...for
multiple bar graphs.....overlapped
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
10. Ø Matplotlib Bar Plot- Side by Side Graph
• import matplotlib.pyplot as plt
• import numpy as np
x=[ “python”, “c”, “c++”, “java”]
y=[80,70,60,82 ]
z=[ 20,30,40,50]
p = [0,1,2,3].....indexing of x
OR
p = np.arange(len(x))...by importing numpy also we can create an array of indexing
width = 0.4
plt.bar(p,y, width, color=“y”,label=“Popularity”)....x replaced with ‘p’
plt.bar(p,z, width, color=“g”, label=“Popularity1”)....x replaced with ‘p’
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
gives number
at x-axis and
is over lapped
11. Ø Matplotlib Bar Plot- Side by Side Graph
• import matplotlib.pyplot as plt
• import numpy as np
x=[ “python”, “c”, “c++”, “java”]
y=[80,70,60,82 ]
z=[ 20,30,40,50]
width = 0.4
p = np.arange(len(x))
p1=[ j+width for j in p]...will create another graph of same width on side
plt.bar(p,y, width, color=“y”,label=“Popularity”)....x replaced with ‘p’
plt.bar(p1,z, width, color=“g”, label=“Popularity1”)....x replaced with ‘p’
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
gives number
at x-axis
12. Ø Matplotlib Bar Plot- Side by Side Graph
• import matplotlib.pyplot as plt
• import numpy as np
x=[ “python”, “c”, “c++”, “java”]
y=[80,70,60,82 ]
z=[ 20,30,40,50]
width = 0.4
p = np.arange(len(x))
p1=[ j+width for j in p]...will create another graph of same width on side
plt.bar(p,y, width, color=“y”,label=“Popularity”)....x replaced with ‘p’
plt.bar(p1,z, width, color=“g”, label=“Popularity1”)....x replaced with ‘p’
plt.xticks(p+width,x)......to show name at x-axisand at right hand side
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
gives name at
RHS
13. Ø Matplotlib Bar Plot- Side by Side Graph
• import matplotlib.pyplot as plt
• import numpy as np
x=[ “python”, “c”, “c++”, “java”]
y=[80,70,60,82 ]
z=[ 20,30,40,50]
width = 0.4
p = np.arange(len(x))
p1=[ j+width for j in p]...will create another graph of same width on side
plt.bar(p,y, width, color=“y”,label=“Popularity”)....x replaced with ‘p’
plt.bar(p1,z, width, color=“g”, label=“Popularity1”)....x replaced with ‘p’
plt.xticks(p+width/2,x,rotation=10)......to show name at x-axis and at
center
plt.legend()
plt.show( )
plt.xlabel (“languages”, fontsize=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note- To apply the label we must apply for legend.
gives name in
rotation
14. Ø Matplotlib Bar Plot- Horizontal Graph
• import matplotlib.pyplot as plt
• import numpy as np
• x=['Python','C','C++', 'Java']
• y=[90,65,82,85]
• z=[23,52,29,20]
• width = 0.8
• p=np.arange(len(x))
• p1=[j+width for j in p]
• plt.barh(p,y, width, color='r')
• plt.bar(p1,z, width, color='k')
• plt.xticks(p+width/2,x,rotation=50)
• plt.show ()
15. Ø Matplotlib Step Plot-
• import matplotlib.pyplot as plt
x=[ ]
y=[ ]
plt.step(x,y,marker= “o”, color= “r”, ms=10, mfc=
“g”)
plt.legend()
plt.grid()
plt.show( )
plt.xlabel (“languages”, font size=10)
plt.ylabel(“No.” ”, font size=10)
plt.title(“Graph1” ”, font size=10)
Note-To align the bars on the right edge pass a negative
width and align='edge'
33. Ø Matplotlib Save Figure-
• import matplotlib.pyplot as plt
x=[ ]
y=[ ]
plt.plot(x,y)
plt.savefig(“fname”, dpi=1000, facecolor= “g”,
transparent=True)
plt.savefig(fname.pdf)......save in format as per
requirement
plt.show( )
Ex-
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[90,65,82,85,80]
plt.plot(x,y)
plt.savefig("line")
plt.show()
Note- File gets saved in a folder location
34. Ø Matplotlib Work With Axes-
• import matplotlib.pyplot as plt
• x=[1,2,3,4,5]
• y=[3,2,1,3,4]
• plt.plot(x,y)
• plt.xticks(x)
• plt.yticks(x)
• plt.show ()
35. Ø Matplotlib Work With Axes-
• import matplotlib.pyplot as plt
• x=[1,2,3,4,5]
• y=[3,2,1,3,4]
• plt.plot(x,y)
• plt.xticks(x,labels=["Python","Java","C","C++","HTML"])
• plt.yticks(x)
• plt.show ()
36. Ø Matplotlib Work With Axes-
• import matplotlib.pyplot as plt
• x=[1,2,3,4,5]
• y=[3,2,1,3,4]
• plt.plot(x,y)
• plt.xlim(0,10)
• plt.show ()
37. Ø Matplotlib Work With Axes-
• import matplotlib.pyplot as plt
• x=[1,2,3,4,5]
• y=[3,2,1,3,4]
• plt.plot(x,y)
• plt.axis([0,10,0,7])
• plt.show ()
38. Ø Text in Matplotlib-
text- Add text at an arbitrary location of the axes.
annotate- Add an annotation with an optional arrow at an
arbitrary location of the axes
xlabel- Add a label to the axes’s along x-axis
ylabel- Add a label to the axes’s along y-axis
title- Add a title to the axes
39. Ø Text in Matplotlib-
Ex-
import matplotlib.pyplot as plt
x=[1,2,3,4,5]
y=[3,2,1,3,4]
plt.plot(x,y)
plt.text(2,3,"java",style="italic",bbox={"facecolor":"c"})
plt.annotate("python",xy=(2,1),xytext=(4,4),arrowprops=dict(facec
olor="green"))
plt.legend(["up"],loc=9,facecolor="red",edgecolor="c",framealpha
=0.5,shadow=True)
plt.show ()
text position on x and y
axis