explanation each code line by line experiment 1 - FarhaKousar1601/DATA-SCIENCE-AND-ITS-APPLICATION-LABORATORY-21AD62- GitHub Wiki
Experiment
Aim
A study was conducted to understand the effect of the number of hours the students spent studying on their performance in the final exams. Write a code to plot a line chart with the number of hours spent studying on the x-axis and score in the final exam on the y-axis. Use a red ‘*’ as the point character, label the axes, and give the plot a title.
Code Explanation
import matplotlib.pyplot as plt
hours = [10, 9, 2, 15, 10, 16, 11, 16]
score = [95, 80, 10, 50, 45, 98, 38, 93]
# Plotting the line chart
plt.plot(hours, score, marker='*', color='red', linestyle='-')
# Adding labels and title
plt.xlabel('Number of Hours Studied')
plt.ylabel('Score in Final Exam')
plt.title('Effect of Hours Studied on Exam Score')
# Displaying the plot
plt.grid(True)
plt.show()
Explanation of Each Line
Importing the Required Library
import matplotlib.pyplot as plt
import
is a statement used to include external libraries or modules in your code.matplotlib.pyplot
is a collection of functions in the popular plotting library Matplotlib, which provides a MATLAB-like interface.as plt
assigns a shorter alias tomatplotlib.pyplot
to simplify its usage in the code.
Defining the Data
hours = [10, 9, 2, 15, 10, 16, 11, 16]
score = [95, 80, 10, 50, 45, 98, 38, 93]
hours
is a list containing the number of hours students spent studying.score
is a list containing the corresponding scores students achieved in the final exam.
Plotting the Line Chart
plt.plot(hours, score, marker='*', color='red', linestyle='-')
plt.plot()
is a function used to create a line chart.hours
is used for the x-axis data.score
is used for the y-axis data.marker='*'
sets the point character to a red asterisk.color='red'
sets the line and marker color to red.linestyle='-'
sets the line style to a solid line.
Adding Labels and Title
plt.xlabel('Number of Hours Studied')
plt.ylabel('Score in Final Exam')
plt.title('Effect of Hours Studied on Exam Score')
plt.xlabel()
sets the label for the x-axis.plt.ylabel()
sets the label for the y-axis.plt.title()
sets the title for the plot.
Displaying the Plot
plt.grid(True)
plt.show()
plt.grid(True)
adds a grid to the plot for better readability.plt.show()
displays the plot on the screen.
Library Definitions
Matplotlib
- Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.
- Pyplot is a module in Matplotlib used for making simple plots like line charts, bar charts, and histograms.
Questions and Answers
import matplotlib.pyplot as plt
do?
What does - It imports the
pyplot
module from thematplotlib
library and assigns it the aliasplt
for easier usage.
hours
and score
?
Why do we use lists for - Lists are used to store multiple values in a single variable, which is useful for holding the data points for plotting.
plt.plot()
?
What is the purpose of plt.plot()
is used to create a line chart with the specified data and formatting options.
How do we add labels and a title to the plot?
- We use
plt.xlabel()
to set the x-axis label,plt.ylabel()
to set the y-axis label, andplt.title()
to set the title of the plot.
How do we display the plot on the screen?
- We use
plt.show()
to render the plot and display it on the screen.