Monday, March 23, 2020

Data Visualization using python libraries | matplotlib I Seaborn | plotl...





Visualization is any technique for creating images, diagrams, or animations to communicate a message.
Types of Data Visualizations :
Explanatory:
Exploratory:

Python Visualisation Libraries
• Matplotlib
o
https://matplotlib.org/
• Pandas built-in plotting
• ggpy
o
https://github.com/yhat/ggpy
• Altair
o
https://altair-viz.github.io/
• Seaborn
o
https://seaborn.pydata.org/
• Plotly
o
https://plot.ly/python/
• Bokeh
o
https://bokeh.pydata.org/en/latest/
• HoloViews
o
http://holoviews.org/
• VisPy
o
http://vispy.org/
• Lightning
o
http://lightning-viz.org/

Visualization methods :
Distribution
It is commonly used at the initial stage of data exploration i.e. when we get started with understanding the variable. Variables are of two types: Continuous and Categorical. For continuous variable, we look at the center, spread, outlier. For categorical variable we look at frequency table.
Histogram : It is used for showing the distribution of continuous variables.
Box-Plot : It is used to display full range of variation from min to max and useful to identify outlier values.
Comparison
It is used to compare values across different categories.
Common charts to represent these information are Bar and Line chart.
Bar Chart : It is used to compare values across different categories
Line Chart : It is used to compare values over quantitative variable
Composition
It is used to show distribution of a variable across categories of another variable
Pie Chart : It can be created by passing the values representing each of the slices of the pie.
Relationship
It is widely used to understand the correlation between two or more continuous variables
Scatter Plot : It clearly shows the relationship between two variables

Demo 1 : Basic Plot
import matplotlib.pyplot as plt
plt.plot([1,2,4],[5,7,4])
plt.show()

Demo 2 : Basic Plot Legend Title Labels
import matplotlib.pyplot as plt
x,y = [1,2,4],[5,7,4]
x2,y2 = [1,2,5],[8,11,5]
plt.plot(x,y, label = "Firstline")
plt.plot(x2,y2, label = "Secondline")
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Graph Title')
plt.legend()
plt.show()

Demo 3 : Bar Chart
import matplotlib.pyplot as plt
x = [2,4,6,8,10]
y = [6,7,8,2,4]
x2 = [1,3,5,7,9]
y2 = [7,8,2,4,2]
plt.bar(x, y, label="FirstBar", color='lightblue')
plt.bar(x2, y2, label="SecondBar", color='c')
plt.xlabel('Bar Number')
plt.ylabel('Bar Height')
plt.title('Graph: Two Bars')
plt.legend()
plt.show()

Demo 4 : Hist Chart
import matplotlib.pyplot as plt
population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,122,130,111,151,115,112,80,75,65,54,44,42,48]
bins = [0,10,20,30,40,50,60,70,80,90,100,120]
plt.hist(population_ages, bins , histtype='bar', rwidth=0.8)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Graph: Hist Chart')
plt.legend()
plt.show()

Demo 5 : Scatter Plot
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8]
y = [5,2,4,2,1,4,5,2]
plt.scatter(x,y,label='skitcat', color='r',s=250)
# google matplotlib marker option
plt.scatter(x,y,label='skitcat', color='r',s=200, marker = '*')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Graph: Scatter Plot')
plt.show()

Demo 6 : Stack Plot
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
plt.plot([],[],color = 'm',label = 'sleeping')
plt.plot([],[],color = 'c',label = 'eating')
plt.plot([],[],color = 'r',label = 'working')
plt.plot([],[],color = 'y',label = 'playing') # linewidth
plt.stackplot(days,sleeping,eating,working,playing ,colors =['m','c','r','y'])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.title('Graph: Stack Plot')
plt.show()

Demo 7 : Pie Charts
import matplotlib.pyplot as plt
days = [1,2,3,4,5]
sleeping = [7,8,6,11,7]
eating = [2,3,4,3,2]
working = [7,8,7,2,2]
playing = [8,5,7,8,13]
slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['m','c','r','g']
plt.pie(slices, labels = activities, colors=cols)
# startangle , shadow= , explode , autopct
plt.title('Graph: Pie Chart')
plt.show()

Demo 8 : Load Data from Files
import numpy as np
x, y = np.loadtxt('DemoData.txt', delimiter=',', unpack=True)
plt.plot(x,y, label='Loaded from file!')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('Graph : Load Data from File')
plt.legend()
plt.show()

Demo 9 : Live Graphs
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
def animate(i):
graph_data = open('DemoData.txt','r').read()
lines = graph_data.split('\n')
xaxis = []
yaxis = []
for line in lines:
if len(line) greaterthan 1:
x, y =line.split(',')
xaxis.append(x)
yaxis.append(y)
ax1.clear()
ax1.plot(xaxis,yaxis)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()