Before getting into the details and visualizing different forms of data, let's first take the initial steps and know about the building blocks which will facilitate the understanding of data visualization better.

So as part of the building blocks, we will be going through some basic operations with different python libraries which give us a good foundation, so that we could build our knowledge of visualization on top of it.
NumPy is the fundamental package for scientific computing with Python. It contains among other things:
import numpy as np
a = np.array([3,4,3])
print(type(a))
print("The shape of a: ",a.shape)
print(a[0],a[1],a[2])
b = np.array([[3,4],[5,6]])
print(type(b))
print("The shape of b: ",b.shape)
print(b[0,0],b[0,1],b[1,0],b[1,1])
c = np.array([[12,42],[43,15],[345,23]])
print(type(c))
print("The shape of c: ",c.shape)
print(c[0,0],c[0,1],c[1,0],c[1,1],c[2,0],c[2,1])
d = np.array([[32,43,11],[11,44,12],[34,65,23]])
print(type(d))
print("The shape of d: ",d.shape)
print(d[0,0],d[0,1],d[0,2],d[1,0],d[1,1],d[1,2],d[2,0],d[2,1],d[2,2])
e = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]],[[19,20,21],[22,23,24],[25,26,27]]])
print(type(e))
print("The shape of e: ",e.shape)
print(e)
f = np.zeros((2,3))
print("The array f has 2 rows and 3 columns:\n",f)
g = np.ones((4,2))
print("The array g has 4 rows and 2 columns:\n",g)
h = np.full((2,2),9)
print("The constant array h:\n",h)
i = np.random.random((3,3))
print("The random array i:\n",i)
#### Creating a 3X4 array
j = np.array([[1,2,3,4],[3,4,5,6],[23,43,11,5]])
print("The array has 3 rows and 4 columns:\n",j)
i = j[:2,1:4]
print("The value of the indexed j values are:\n",i)
print("the values of array j at 0th row and 1st column:\n",j[0,1])
## Modifying the sliced array i
i[0,0] =100
print("the values of array j at 0th row and 1st column:\n",j[0,1])
## Integer datatype
j = np.array([1,2])
print("The datatype of j is:",j.dtype)
##Float Datatype
k = np.array([1.0,2.4])
print("The datatype of k is:",k.dtype)
## typecasting
l = np.array([1.0,2.0],dtype = np.int64)
print("The typecasted datatype of l:",l.dtype)
X = np.array([[1,2,3,4],[3,5,6,1]])
Y = np.array([[4,5,1,9],[7,4,1,9]])
print("The approach-1 for Element-wise addition is:\n",X + Y)
print("The approach-2 for Element-wise addition is :\n",np.add(X,Y))
print("The approach-1 for Element-wise substraction is:\n",X - Y)
print("The approach-2 for Element-wise substraction is :\n",np.subtract(X,Y))
print("The approach-1 for Element-wise product is:\n",X * Y)
print("The approach-2 for Element-wise product is :\n",np.multiply(X,Y))
print("The approach-1 for Element-wise division is:\n",X / Y)
print("The approach-2 for Element-wise division is :\n",np.divide(X,Y))
print("The approach for Element-wise square-root is :\n",np.sqrt(X))
x = np.array([[1,2],[3,4]])
print("The normal sum is:",np.sum(x))
print("The column wise addition is:",np.sum(x,axis = 0))
x = np.array([[1,2],[3,4]])
print("The normal sum is:",np.sum(x))
print("The column wise addition is:",np.sum(x,axis = 1))
print("The overall mean is: ",np.mean(x))
print("The columnwise mean is: ",np.mean(x,axis=0))
print("The rowwise mean is: ",np.mean(x,axis=1))
Numpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. SciPy builds on this, and provides a large number of functions that operate on numpy arrays and are useful for different types of scientific and engineering applications.
For this course, we will primariyl be using the SciPy.Stats sub-library.
The SciPy.Stats module contains a large number of probability distributions as well as a growing library of statistical functions such as:
from scipy import stats
import numpy as np
print(stats.norm.rvs(size = 15))
Matplotlib is a plotting library. In this section give a brief introduction to the matplotlib.pyplot module.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,3*np.pi,0.1)
print(x)
y = np.sin(x)
plt.plot(x,y)
plt.show()
x = np.arange(0,3*np.pi,0.1)
y_sin = np.sin(x)
y_cosin = np.cos(x)
plt.plot(x,y_sin)
plt.plot(x,y_cosin)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sine and Cosine curve')
plt.legend(['Sine','Cosine'])
plt.show()
import numpy as np
import matplotlib.pylab as plt
## Compute the x and y coordinate for sine and cosine curve
x = np.arange(0,4*np.pi,0.5)
y_sin = np.sin(x)
y_cos = np.cos(x)
## Let's set up a grid for making subplots within those grids with 2 rows and 3 columns.
plt.subplot(2,3,1)
## Lets make the first plot
plt.plot(x,y_sin)
plt.title("1st-sine curve")
plt.subplot(2,3,2)
## Lets make the 2nd plot
plt.plot(x,y_cos)
plt.title("2nd-cos curve")
plt.subplot(2,3,3)
## Lets make the third plot
plt.plot(x,y_sin)
plt.title("3rd-sine curve")
plt.subplots_adjust(top = 0.99, bottom=0.01, hspace=.5, wspace=0.4)
plt.subplot(2,3,4)
## Lets make the forth plot
plt.plot(x,y_cos)
plt.title("4th-sine curve")
plt.subplot(2,3,5)
## Lets make the fifth plot
plt.plot(x,y_sin)
plt.title("5th-sine curve")
plt.subplot(2,3,6)
## Lets make the sixth plot
plt.plot(x,y_sin)
plt.title("6th-sine curve")
import seaborn as sns
import pandas as pd
df = pd.read_csv("Admission_Predict.csv")
df.head()
df['Chance of Admit '].unique()
sns.lmplot(x = 'GRE Score',y = 'TOEFL Score',data = df)
plt.show()
sns.lmplot(x='GRE Score',y='TOEFL Score',data=df,fit_reg=False,hue ='University Rating')
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
ax = sns.scatterplot(x="total_bill", y="tip", data=tips)
tips
sns.scatterplot(x='total_bill',y='tip',data = tips)
plt.show()
In the below plot we use hue argument.
sns.scatterplot(x="total_bill",y="tip",data = tips, hue='time')
plt.show()
Here we use:
sns.scatterplot(x="total_bill", y="tip",hue="time", style="time", data=tips)
plt.show()
sns.scatterplot(x="total_bill",y="tip",hue="day",style='time',data=tips)
plt.show()
Let's first understand what is size.
size: It is basically grouping variable that will produce points with different sizes. Can be either categorical or numeric, although size mapping will behave differently in latter case.
sns.scatterplot(x='total_bill',y='tip',size='size',data=tips)
plt.show()
sns.scatterplot(x='total_bill',y='tip',size=)
cmap = sns.cubehelix_palette(dark=.1, light=.9, as_cmap=True)
sns.scatterplot(x="total_bill",y='tip',size='size',hue='size',palette=cmap,data=tips)
plt.show()
That means, the smallest size 1 has the least size = 20 and the largest size 6 has the size =200.
We also define the palette by using the below code:
cmap = sns.cubehelix_palette(dark=.1, light=.8, as_cmap=True) Based on the value of the dark and light argument of this function, we can vary the hue_norm values. The smaller the value of dark, the larger is the range for the hue_norm. This means if the dark = 0.1 then the range of hue_norm also increases.
Note: Also remember, we can use the sizes argument only when we specify the size argument.
cmap = sns.cubehelix_palette(dark=.1, light=.8, as_cmap=True)
sns.scatterplot(x="total_bill",y='tip',size='size',hue='size',sizes = (20,400),hue_norm=(0,8),
palette=cmap,legend ='full',data=tips)
plt.show()
sns.scatterplot(x='total_bill',y='tip',hue='day',size='smoker',sizes = (40,200),palette='Set1',data=tips)
Here we are using
Note: Also remember, we can use the sizes argument only when we specify the size argument.
markers = {"Lunch":'s',"Dinner":'X'}
sns.scatterplot(x="total_bill",y="tip",style='time',markers=markers,
palette="Set1",data =tips,hue='time',size = 'time',sizes=(200,120))
iris = sns.load_dataset("iris")
sns.scatterplot(x=iris.sepal_length,y=iris.sepal_width,
hue=iris.species,style=iris.species)
plt.show()
sns.relplot(x="total_bill",y="tip",col="time",hue="day",style="day",
kind="scatter",data = tips,size ="day",sizes=(80,200))
plt.show()
import pandas as pd
df = pd.read_csv("student.csv")
df.head()
df['Age'].unique()
sns.swarmplot(x='Gender',y='Age',data=df)
plt.show()
df1 = df[(df.Age >= 19) &(df.Age<22)]
df1.head()
sns.swarmplot(x='Gender',y='Age',data=df1)
plt.show()
data = pd.read_csv("Cartwheeldata.csv")
data.head()
sns.swarmplot(x ='Gender',y='CWDistance',data=data)
import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])
sns.swarmplot(x="day",y='total_bill',data= tips)
plt.show()
sns.swarmplot(x='total_bill',y='day',data=tips)
plt.show()
sns.swarmplot(x="day",y="total_bill",hue='sex',data=tips)
plt.show()
Here using
sns.swarmplot(x="day",y="total_bill",hue="smoker",data = tips, palette="Set1",dodge = True)
sns.swarmplot(x='time',y='tip',data=tips)
plt.show()
sns.swarmplot(x='time',y='tip',palette='Set1',order=['Dinner','Lunch'],data = tips)
plt.show()
sns.swarmplot(x='time',y='tip',palette='Set1',data=tips,size = 7)
plt.show()
If you notice here in the boxplot, the whis=np.inf ensures that the whisker of the boxplot extends till the last outlier. You can see that in the below plot the whicker connects to the last point which is the outlier where as this doesnot happen if we are not using whis=np.inf. This is evident in the plot no 10.
sns.boxplot(x='tip',y='day',data=tips,whis=np.inf)
sns.swarmplot(x='tip',y='day',data=tips,color=".1")
plt.show()
sns.boxplot(x='tip',y='day',data=tips)
plt.show()
sns.catplot(x='sex',y ='total_bill',hue='smoker',col='time',data=tips,kind='swarm',height = 6, aspect=.8)
plt.show()
import seaborn as sns
sns.set(style = "whitegrid")
tips = sns.load_dataset("tips")
sns.boxplot(x=tips["total_bill"])
plt.show()
sns.boxplot(x="day",y="total_bill",data = tips)
plt.show()
sns.boxplot(x="day",y="total_bill",hue="smoker",palette="Set1",data =tips)
plt.show()
sns.boxplot(x="day",y="total_bill",hue="time",data =tips,linewidth=1)
plt.show()
sns.boxplot(x="time",y="tip",data=tips,order=['Dinner','Lunch'])
plt.show()
iris = sns.load_dataset("iris")
iris.head()
sns.boxplot(data=iris,orient='h',palette='Set1')
plt.show()
Let's take such example where we want to check the total_bill based on the condition that the days are weekend.
In the below plot you can see that the data has been categorized as True ans False based on whether it is weekend or weekday
tips["weekend"] = tips["day"].isin(["Sat","Sun"])
sns.boxplot(x="day",y="total_bill",hue="weekend",data = tips)
plt.show()
sns.catplot(x="sex",y="total_bill",hue="smoker",col="time",
data = tips,kind="box",height = 7, aspect = .8)
plt.show()
data = pd.read_csv("Cartwheeldata.csv")
data.head()
sns.boxplot(data = data.loc[:,["Age","Height","Wingspan","CWDistance","Score"]])
plt.show()
sns.boxplot(data= data.loc[data['Gender']=='M',["Age","Height","Wingspan","CWDistance","Score"]])
plt.show()
sns.boxplot(data=data.loc[data['Gender']=='F',["Age","Height","Wingspan","CWDistance","Score"]])
plt.show()
sns.boxplot(data = data.loc[data['Gender']=='M',["Score"]])
plt.show()
sns.boxplot(data = data.loc[data['Gender']=='F',["Score"]])
plt.show()
df = pd.read_csv("student.csv")
df.head()
## Distribution plot
sns.distplot(df.Age)
plt.show()
data = pd.read_csv("Cartwheeldata.csv")
data.head()
sns.distplot(data.CWDistance)
plt.show()
df = pd.read_csv("student.csv")
df.head()
sns.countplot(x='Gender',data=df)
plt.xticks(rotation=45)
plt.show()
data = pd.read_csv("Cartwheeldata.csv")
data.head()
sns.countplot(x='Gender',data = data)
plt.xticks(rotation=0)
plt.show()