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

shivansh_exp4

The document outlines an experiment using Matplotlib to create bar and scatter plots for data visualization. It explains the purpose of these plots, provides example code for generating them, and discusses the importance of visualization in analyzing data relationships. Additionally, it includes FAQs addressing common questions about using Matplotlib for data visualization.
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)
4 views3 pages

shivansh_exp4

The document outlines an experiment using Matplotlib to create bar and scatter plots for data visualization. It explains the purpose of these plots, provides example code for generating them, and discusses the importance of visualization in analyzing data relationships. Additionally, it includes FAQs addressing common questions about using Matplotlib for data visualization.
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
You are on page 1/ 3

Topic: Python for Data Science

Experiment No: 4

Create a bar plot and scatter plot to visualize relationships in a dataset using Matplotlib.
Aim:
To create a bar plot and scatter plot using Matplotlib to visualize relationships in a dataset.

Objective:
Learn how to use Matplotlib to create bar plots and scatter plots.
Understand how visualization helps in analyzing relationships and distributions in data.
Gain hands-on experience in working with Python data visualization libraries.

Theory:

What is Matplotlib?

Matplotlib is a popular Python library used for data visualization. It provides various plotting
functions to represent data trends, distributions, and relationships.

Bar Plot:

A bar plot is used to compare categorical data. It displays data using rectangular bars, where the
height represents the magnitude of each category.

● plt.bar(x, y) creates a bar plot.

Scatter Plot:

A scatter plot is used to visualize relationships between two numerical variables. It helps in
identifying correlations and patterns.

● plt.scatter(x, y) creates a scatter plot.

Python Program:
import matplotlib.pyplot as plt
students = ['ABC', 'DEF', 'IJK']
marks = [85, 90, 78]
study_hours = [5, 6, 4]
plt.figure(figsize=(10, 5))
plt.bar(students, marks, color='skyblue')
plt.title('Student Marks Comparison')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.show()
plt.figure(figsize=(10, 5))
plt.scatter(study_hours, marks, color='green')
plt.title('Study Hours vs Marks')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.show()
Conclusion:

● Successfully created a bar plot to compare student marks.


● Created a scatter plot to analyze the relationship between study hours and marks.
● Observed how Matplotlib provides effective ways to visualize data relationships.

Output (Screenshots):

1. Bar Plot (Students vs. Marks):


2. Scatter Plot (Study Hours vs. Marks):

Frequently Asked Questions (FAQs):

1. Why use Matplotlib for visualization?


Matplotlib is used for visualization because it provides flexibility, customization, and support
for various types of plots like line graphs, bar charts, and scatter plots. It helps in analyzing
and presenting data effectively.

2. How do I change the color of the bars in the bar plot?


To change the color of the bars in a bar plot using Matplotlib or Pandas, you can use the
color parameter.

3. How do I add data labels on top of bars in the bar plot?


To add data labels on top of bars in a bar plot, you can use the text() function from Matplotlib
to display the values.

4. How do I add a trendline to a scatter plot?


To add a trendline to a scatter plot, you can use NumPy to calculate the linear regression line
(best fit line), and then plot it alongside your scatter plot.

5. Can I save the plots as images?


Yes, we can save plots as images using plt.savefig("plot.png")

You might also like