0% found this document useful (0 votes)
6 views3 pages

Python Code For Graphs

The document outlines the process of analyzing survey data using Python, including loading data, creating a correlation heatmap, and checking linearity with scatterplots. It also discusses fitting a linear regression model and assessing homoscedasticity through residual plots, as well as visualizing categorical data with pie charts. The analysis focuses on variables such as stress, procrastination, anxiety, and self-regulation among students.

Uploaded by

tayybaaliahmed3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Python Code For Graphs

The document outlines the process of analyzing survey data using Python, including loading data, creating a correlation heatmap, and checking linearity with scatterplots. It also discusses fitting a linear regression model and assessing homoscedasticity through residual plots, as well as visualizing categorical data with pie charts. The analysis focuses on variables such as stress, procrastination, anxiety, and self-regulation among students.

Uploaded by

tayybaaliahmed3
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Loading the data and preparing the environment


First, I imported the necessary libraries and read the data from an Excel file.
The file contained all the survey responses.

import pandas as pd
import seaborn as sns
import [Link] as plt
import [Link] as sm

# Loading the data


df = pd.read_excel("thesis_data.xlsx")

2. Correlation heatmap
I selected the five main variables, CGPA, stress, anxiety, self-regulation, and
procrastination – and calculated their correlations. Then I created a grayscale
heatmap. Darker grey means a stronger relationship. This heatmap is suitable
for black-and-white printing.

# Selecting the five main variables


main_cols = [’CGPA’, ’Stress_Scale’, ’Anxiety_Scale’,
’SelfRegulation_Scale’, ’Procrastination_Scale’]

# Computing correlation matrix


corr = df[main_cols].corr()

# Drawing the heatmap


[Link](figsize=(8, 6))
[Link](corr, annot=True, cmap="Greys", fmt=".2f",
square=True, linewidths=0.5, cbar_kws={"shrink": 0.8})
[Link]("Correlation Heatmap (Absolute Values)", fontsize=14)
plt.tight_layout()
[Link]("correlation_heatmap.png", dpi=300)
[Link]()

3. Scatterplot for linearity (Stress vs Procrastination)


Before running regressions, I checked whether the relationship between stress
and procrastination was straight (linear). I made a scatterplot with a regression
line. The points clearly follow a straight line, so the linearity assumption is
satisfied.

# Extract the two variables


X = df["Stress_Scale"]
y = df["Procrastination_Scale"]

1
# Creating scatterplot with regression line
[Link](figsize=(8, 6))
[Link](x=X, y=y,
scatter_kws={’alpha’:0.6, ’color’:’#2c7bb6’},
line_kws={’color’:’#d7191c’, ’linewidth’:2})
[Link]("Stress Score", fontsize=12)
[Link]("Procrastination Score", fontsize=12)
[Link]("Scatterplot of Stress vs Procrastination with Regression Line", fontsize=14)
[Link](True, linestyle=’--’, alpha=0.5)
plt.tight_layout()
[Link]("scatter_stress_procrast.png", dpi=300)
[Link]()

4. Residual plot for homoscedasticity


After fitting a simple linear regression (Stress → Procrastination), I saved the
residuals and the predicted values. I then plotted the residuals against the
predicted values. The points are randomly scattered around zero, with no funnel
shape. This tells us that the variance of errors is constant (homoscedasticity).

# Fitting the regression


X_const = sm.add_constant(X)
model = [Link](y, X_const).fit()
residuals = [Link]
predicted = [Link]

# Residual plot
[Link](figsize=(8, 6))
[Link](predicted, residuals, alpha=0.6, color=’#2c7bb6’)
[Link](y=0, color=’#d7191c’, linestyle=’-’, linewidth=1.5)
[Link]("Predicted Procrastination Score", fontsize=12)
[Link]("Residuals", fontsize=12)
[Link]("Residual Plot for Regression of Procrastination on Stress", fontsize=14)
[Link](True, linestyle=’--’, alpha=0.5)
plt.tight_layout()
[Link]("residual_stress_procrast.png", dpi=300)
[Link]()

5. Pie charts for categorical levels


Finally, I made four pie charts to show the percentage of students in each
category (low, moderate, high, etc.) for stress, procrastination, anxiety, and
self-regulation. I used a soft pastel colour palette so the charts look profes-
sional.

# Data from the frequency tables

2
stress_counts = [40, 157, 80]
stress_labels = [’Low’, ’Moderate’, ’High’]

procrast_counts = [83, 117, 77]


procrast_labels = [’Low’, ’Moderate’, ’High’]

anxiety_counts = [19, 68, 148, 42]


anxiety_labels = [’Low’, ’Moderate’, ’High’, ’Very High’]

selfreg_counts = [44, 170, 63]


selfreg_labels = [’Low’, ’Moderate’, ’High’]

# Colour palette
colours = [’#ffb3ba’, ’#b5e6c9’, ’#ffe6a7’, ’#b5d3e7’, ’#e0b5d5’]

# Function to draw and save a pie chart


def make_pie(counts, labels, title, filename, colour_subset):
[Link](figsize=(6, 5))
wedges, texts, autotexts = [Link](
counts, labels=labels, autopct=’%1.1f%%’, startangle=90,
colors=colour_subset[:len(counts)], textprops={’fontsize’: 10}
)
for autotext in autotexts:
autotext.set_color(’white’)
autotext.set_fontweight(’bold’)
[Link](title, fontsize=12, fontweight=’bold’)
plt.tight_layout()
[Link](filename, dpi=300, bbox_inches=’tight’)
[Link]()

# Generating the four pie charts


make_pie(stress_counts, stress_labels, ’Stress Levels (N=277)’,
’stress_pie.png’, colours)
make_pie(procrast_counts, procrast_labels, ’Procrastination Levels (N=277)’,
’procrastination_pie.png’, colours)
make_pie(anxiety_counts, anxiety_labels, ’Anxiety Levels (N=277)’,
’anxiety_pie.png’, colours)
make_pie(selfreg_counts, selfreg_labels, ’Self-regulation Levels (N=277)’,
’selfreg_pie.png’, colours)

You might also like