# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# Import the required libraries: Pandas, Numpy, Matplotlib and   Seaborn

import pandas as pd          
import numpy as np 
import seaborn as sns # For mathematical calculations
import matplotlib.pyplot as plt  # For plotting graphs
from datetime import datetime    # To access datetime
from pandas import Series # To work on series
# %matplotlib inline
import warnings                   # To ignore the warnings
warnings.filterwarnings("ignore")

# +
# Let us load and read the data from the csv file 

df=pd.read_csv("StudentsPerformance.csv")
df.columns
# -

df.sample(2)

df.info()

df["Total_Marks"]=df["math score"]+df["reading score"]+df["writing score"]
df.head(2)

df["Percentage"]=df["Total_Marks"]*1/3
df.head(2)

# +
# Top 10  Best Performed Students Overall

top_10 =df.sort_values(["Percentage"], ascending=[False])[:10]
top_10
# -

# #### Seaborn Categorical Plots in Python

# To demonstrate the various categorical plots used in Seaborn, we will use the in-built dataset present in the seaborn library which is the ‘tips’ dataset.
#
# barplot
#
# countplot
#
# boxplot
#
# violinplot
#
# striplot
#
# swarmplot
#
#

# +
# Load data using Seaborn Library

sf=sns.load_dataset('tips')
sf.head()
# -

# ### 1. Bar Plot

# +
# Bar plot visualizing the  top 10 performers

sns.barplot(x='gender',y='Percentage',data=top_10)
# -

sns.barplot(x='gender',y='Percentage',hue="parental level of education",data=top_10, palette="Set2")

# Here parameters x, y refers to the name of the variables in the dataset provided in parameter ‘df’.

#  ### 2. Count Plot
#  
# This is essentially the same as barplot except the estimator is explicitly counting the number of occurrences. Which is why we only pass the x value.

# +
# Count plot 
# counts the number of occurences of the gender column in the top 10 best performers

sns.countplot(x='gender',data=top_10)
# -

sns.countplot(x='gender', hue="Percentage", data=top_10)

#  ### 3. Box Plot
#
# A box plot (or box-and-whisker plot) shows the distribution of quantitative data in a way that facilitates comparisons between variables or across levels of a categorical variable. The box shows the quartiles of the dataset while the whiskers extend to show the rest of the distribution, except for points that are determined to be “outliers” using a method that is a function of the inter-quartile range.

# +
# Categorical Variable vs Numerical Continuous Variable

# Box plot Visualization
# Comparing the  student performance  against the gender column

sns.boxplot(x='gender',y='Percentage',data=top_10, palette='rainbow')
# -

# It’s also possible to add a nested categorical variable with the hue parameter.

# +
# Categorical Variable vs Categorical Variable Vs Numerical Continuous Variable
# Adding  a nested categorical variable with the Hue Parameter
# It’s also possible to add a nested categorical variable with the hue parameter.


sns.boxplot(x="gender",y="Percentage",hue="parental level of education",data=top_10, palette="coolwarm")
# -

# #### Violin plot
#
# A violin plot plays a similar role as a box and whisker plot. It shows the distribution of quantitative data across several levels of one (or more) categorical variables such that those distributions can be compared. Unlike a box plot, in which all of the plot components correspond to actual datapoints, the violin plot features a kernel density estimation of the underlying distribution.

sns.violinplot(x='gender',y='Percentage',data=top_10, palette='rainbow')

#  hue can also be applied to violin plot.
#  
#  Output gives:

sns.violinplot(x="gender",y="Percentage",hue="parental level of education",data=top_10, palette='Set1')

# #### Strip plot AND swarn plot
#
# The stripplot will draw a scatterplot where one variable is categorical. A strip plot can be drawn on its own, but it is also a good complement to a box or violin plot in cases where you want to show all observations along with some representation of the underlying distribution.
#
# The swarmplot is similar to stripplot(), but the points are adjusted (only along the categorical axis) so that they don’t overlap. This gives a better representation of the distribution of values, although it does not scale as well to large numbers of observations (both in terms of the ability to show all the points and in terms of the computation needed to arrange them).

sns.stripplot(x="gender", y="Percentage", data=top_10)

sns.stripplot(x="gender",y="Percentage",data=top_10,jitter=True,hue='parental level of education',palette='Set1')

# #### Command for swarm plot

sns.swarmplot(x="gender", y="Percentage", data=top_10)

sns.swarmplot(x="gender",y="Percentage",hue='parental level of education',data=top_10, palette="Set1", split=True)

# ### DONE

# +
# Box plot for the entire whole dataframe with orient='h'

sns.boxplot(data=df,palette='coolwarm',orient='h')

# +
# Categorical Variable vs Categorical Variable Vs Numerical Continuous Variable
# Adding  a nested categorical variable with the Hue Parameter
# It’s also possible to add a nested categorical variable with the hue parameter.


sns.boxplot(x="gender",y="Percentage",hue="parental level of education",data=df, palette="coolwarm")
# -

sns.violinplot(x="gender",y="Percentage",hue="parental level of education",data=df, palette='Set1')

sns.stripplot(x="gender",y="Percentage",data=df,jitter=True,hue='parental level of education',palette='Set1')

# +
# Categorical Variable vs Numerical Continuous Variable

# Box plot Visualization
# Comparing the total performance of all the students against the gender column

sns.boxplot(x='gender',y='Percentage',data=df, palette='rainbow')
# -

sns.stripplot(x="gender", y="Percentage", data=df)

StarcoderdataJupyterScriptsDedupFiltered