4.7 Introduction to Data
Visualization
Data Visualization is
the process of representing data through charts and graphs to make it easier to
understand, analyze and interpret. It helps us identify patterns, trends
and relationships in a simple and meaningful way.
Python provides
various libraries, such as Matplotlib, Seaborn and Plotly, to create
simple and advanced graphical representations of data.
Importance of Data Visualization
1.
Easy
Understanding:
Helps people understand large amounts of data easily through charts and graphs.
2.
Identify
Patterns: Helps
find repeated arrangements in data, such as sales increasing every Dashain.
3.
Identify
Trends: Helps
identify changes in data over time, such as the number of computer users
increasing every year.
4.
Identify
Relationships:
Helps find connections between different sets of data, such as the relationship
between students' study hours and their marks.
5.
Better
Decision-Making:
Helps businesses and professionals make better decisions by examining data.
6.
Clear
Communication:
Helps present and share information in a simple and understandable way.
Python Libraries
for Data Visualization
Python provides
different libraries for data visualization. Each library has its own features
and is used to create different types of graphs and charts. Some popular Python
libraries are:
1. Matplotlib: Used
to create basic graphs such as bar charts, line graphs and pie charts.
2. Seaborn: Used to
create attractive and colorful statistical graphs easily.
3. Plotly: Used to
create interactive graphs that allow users to zoom, hover and explore data.
Matplotlib
Matplotlib is one of the most popular Python
plotting libraries used for data visualization. It is a low-level library with
a MATLAB-like interface that provides flexibility to create and customize
different types of graphs, although it may require writing more code.
Installation
of Matplotlib
Using
pip: pip install
matplotlib
Using
conda: conda
install matplotlib
Uses
of Matplotlib
Matplotlib is suitable for creating
different types of graphs, such as line charts, bar charts, pie charts, histograms,
scatter plots
Importing
Matplotlib
The
pyplot module of Matplotlib can be imported using the following statement:
import
matplotlib.pyplot as plt
Here:
- matplotlib
is the library name.
- pyplot
is the module used for creating graphs.
- plt
is the alias (short name) of pyplot.
Scatter
Plot
A Scatter Plot is a type of
graph that uses dots to represent the relationship between two variables. Each
dot represents a pair of values (one X-value and one Y-value). It helps
identify patterns and understand how two variables are related. Matplotlib
provides the scatter() method to create a scatter plot.
Program:
Create a Scatter Plot Using Matplotlib
import
matplotlib.pyplot as plt
hours
= [1, 2, 3, 4, 5]
marks
= [40, 50, 60, 75, 90]
plt.scatter(hours,
marks, color='blue', marker='o')
plt.title("Study
Hours vs Marks")
plt.xlabel("Study
Hours")
plt.ylabel("Marks")
plt.show()
Bar
Chart
A Bar Chart is a type of graph that
uses rectangular bars to represent data in different categories. The height or
length of each bar represents the corresponding data value. It helps compare
values among different categories easily. The bar() method of Matplotlib is
used to create a bar chart.
Program: Create a Bar Chart Using
Matplotlib
# Importing the libraries
import pandas as pd
import matplotlib.pyplot as plt
# Reading the dataset
data =
pd.read_csv("Stu_data.csv")
# Creating bar chart
plt.bar(data['Name'],
data['Marks'], color='skyblue')
# Adding title and labels
plt.title("Student
Marks")
plt.xlabel("Name")
plt.ylabel("Marks")
# Displaying the graph
plt.show()
Difference
Between Scatter Plot and Bar Chart
|
Scatter Plot
|
Bar Chart
|
|
Uses individual dots to represent
data.
|
Uses rectangular bars to
represent data.
|
|
Shows relationships between
variables.
|
Compares different data
categories.
|
|
Created using scatter().
|
Created using bar().
|
SEABORN
Seaborn is a Python
library used to create attractive and informative charts. It is built on top of
Matplotlib and works well with Pandas data. It supports different types of
plots, such as line plots, bar plots and heatmaps. It helps visualize patterns and
relationships in data and create complex charts using fewer lines of code.
Note: Matplotlib provides
the basic graph-making functionality. Seaborn uses Matplotlib and adds attractive designs,
colours and easier ways to create statistical charts.
Types of Charts Supported by
Seaborn
i. Line Plots: Used to
represent trends and changes in data.
ii. Bar Plots: Used to
represent and compare data categories.
iii. Heatmaps: Use different
colours to represent data values.
Installation of Seaborn
Seaborn can be installed using the
following command in the command prompt:
pip install seaborn
Working Environment
Seaborn works best in Jupyter
Notebook or IPython, which display charts more clearly.
Line Plot
A line plot is a type
of graph that represents data points connected by lines. It is used to show
patterns, trends and changes in data.
The lineplot() method is used to
create a line plot in Seaborn.
Basic Syntax
sns.lineplot(x='column1',
y='column2', data=dataset)
Where:
- x
represents the X-axis data.
- y
represents the Y-axis data.
- data
specifies the dataset used to create the plot.
Practical Program: Line Plot Using
Seaborn
# Importing the libraries
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Reading the database
dataset =
pd.read_csv("Stu_data.csv")
# Creating a line plot
sns.lineplot(x='Name', y='Marks',
data=dataset)
# Displaying the graph
plt.show()
PLOTLY
1. Introduction to Plotly
Plotly is an
open-source Python library used to create interactive charts. It supports
different types of graphs, such as 3D charts, scientific and statistical
charts, and financial charts. Plotly graphs can be viewed in Jupyter notebooks
and web browsers or saved as HTML files. It also allows users to interact with
charts by clicking, zooming and editing them.
Installation of Plotly
Plotly can be installed using the
following command in the command prompt or terminal:
pip install plotly
Scatter Plot Using Plotly
# Importing the libraries
import pandas as pd
import plotly.express as px
# Reading the CSV dataset through
Pandas
dataset =
pd.read_csv("tips.csv")
# Creating a scatter plot
graph = px.scatter(dataset,
x="total_bill", y="size", color="smoker")
# Displaying the chart
graph.show()
Line Chart Using Plotly
A line chart in Plotly is used to
display data points connected by straight lines. It helps visualize changes and
trends in data. Each data point is represented as a vertex (point) on the
chart. Plotly makes line charts interactive and easy to customize.
Program: Create a Line Chart Using
Plotly
# Importing the libraries
import plotly.express as px
import pandas as pd
# Reading the database
data =
pd.read_csv("stu_data.csv")
# Creating a line chart
fig = px.line(data, y='Name',
color='Gender')
# Displaying the chart
fig.show()
Bar Chart Using Plotly
A bar chart in Plotly
is used to represent and compare data using rectangular bars. The height of
each bar represents its corresponding value. Plotly Express provides the bar()
function to create interactive bar charts.
Program: Create a Bar Chart Using
Plotly
# Importing the libraries
import plotly.express as px
import pandas as pd
# Reading the database
data =
pd.read_csv("Stu_data.csv")
# Creating a bar chart
fig = px.bar(data, x='Name',
y='Marks', color='Gender')
# Displaying the chart
fig.show()
Common Marker Symbols
Markers are symbols used to show
data points on a graph.
- 'o'
– Circle
- '*'
– Star
- '^'
– Upward triangle
- 's'
– Square
- 'D'
– Diamond
- '+'
– Plus
Common Line Styles
Line styles are used to change the
appearance of lines in a graph.
- '-'
– Solid line
- '--'
– Dashed line
- ':'
– Dotted line
- '-.'
– Dash-dot line
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='red',
marker='o',
linestyle='--', linewidth=2)
plt.title("Student
Marks")
plt.xlabel("Name")
plt.ylabel("Marks")
plt.grid()
plt.show()
Explanation of the Program
|
Code
|
Function
|
|
plt.figure(figsize=(8, 5))
|
Sets the graph size (width 8,
height 5).
|
|
plt.plot(x, y)
|
Creates a line graph using X and
Y data.
|
|
color='red'
|
Changes the line colour to red.
|
|
marker='o'
|
Shows circle symbols at data
points.
|
|
linestyle='--'
|
Makes the line dashed.
|
|
linewidth=2
|
Sets the line thickness to 2.
|
|
plt.title("Student
Marks")
|
Adds a title to the graph.
|
|
plt.xlabel("Name")
|
Labels the X-axis as Name.
|
|
plt.ylabel("Marks")
|
Labels the Y-axis as Marks.
|
|
plt.grid()
|
Displays grid lines.
|
|
plt.show()
|
Displays the graph.
|
PYTHON DATA VISUALIZATION
20 EXAM-BASED PROGRAMMING QUESTIONS
Using Pandas and Matplotlib
========================================
SECTION A: LINE CHART (Q1–Q5)
========================================
Q1. MONTHLY SALES TREND
File: sales.csv
Month,Sales
Jan,12000
Feb,15000
Mar,18000
Apr,14000
May,20000
Write a Python program to:
1. Import Pandas and Matplotlib.
2. Read the CSV file.
3. Create a line chart using
plt.plot().
4. Use a red line with circle
markers.
5. Set the line width to 2.
6. Add the title "Monthly
Sales Trend".
7. Label the X-axis as Month and
Y-axis as Sales.
8. Display the chart.
Q2. WEEKLY TEMPERATURE CHANGES
File: temperature.csv
Day,Temperature
Sunday,20
Monday,25
Tuesday,28
Wednesday,24
Thursday,30
Write a Python program to:
1. Read the CSV file using Pandas.
2. Create a line chart using
plt.plot().
3. Use a blue line with square
markers.
4. Set the line style to dashed
(--).
5. Add the title "Weekly
Temperature Changes".
6. Label both axes.
7. Display the chart.
Q3. STUDENT PERFORMANCE
File: students.csv
Student,Marks
Ram,65
Sita,75
Hari,80
Gita,90
Rita,85
Write a Python program to:
1. Read the CSV file.
2. Create a line chart using
plt.plot().
3. Use a green line with circle
markers.
4. Set the line width to 2.
5. Add the title "Student
Performance".
6. Label both axes.
7. Add a grid.
8. Display the chart.
Q4. POPULATION GROWTH
File: population.csv
Year,Population
2021,10000
2022,12000
2023,15000
2024,18000
2025,22000
Write a Python program to:
1. Read the CSV file.
2. Create a line chart using
plt.plot().
3. Use a purple line with circle
markers.
4. Set the line style to dashed.
5. Add the title "Population
Growth".
6. Label both axes.
7. Display the chart.
Q5. MONTHLY WEBSITE VISITORS
File: visitors.csv
Month,Visitors
Jan,500
Feb,700
Mar,650
Apr,900
May,1200
Write a Python program to:
1. Read the CSV file.
2. Create a line chart using
plt.plot().
3. Use an orange line with diamond
markers.
4. Set the line width to 3.
5. Add the title "Monthly
Website Visitors".
6. Label both axes.
7. Add a grid.
8. Display the chart.
========================================
SECTION B: BAR CHART (Q6–Q10)
========================================
Q6. SUBJECT-WISE MARKS
File: marks.csv
Subject,Marks
English,75
Mathematics,85
Science,90
Computer,95
Nepali,80
Write a Python program to:
1. Read the CSV file using Pandas.
2. Create a bar chart using
plt.bar().
3. Set the bar colour to blue.
4. Add the title "Subject-wise
Marks".
5. Label both axes.
6. Display the chart.
Q7. PRODUCT SALES
File: products.csv
Product,Sales
Laptop,20
Mobile,35
Keyboard,40
Mouse,50
Printer,15
Write a Python program to:
1. Read the CSV file.
2. Create a bar chart using
plt.bar().
3. Use green bars.
4. Add the title "Product
Sales".
5. Label both axes.
6. Display the chart.
Q8. NUMBER OF STUDENTS IN EACH
CLASS
File: classes.csv
Class,Students
8,35
9,40
10,45
11,30
12,25
Write a Python program to:
1. Read the CSV file.
2. Create a bar chart using
plt.bar().
3. Use orange bars.
4. Add the title "Number of
Students in Each Class".
5. Label both axes.
6. Display the chart.
Q9. MONTHLY EXPENSES
File: expenses.csv
Category,Amount
Food,12000
Rent,15000
Education,8000
Transport,3000
Others,2000
Write a Python program to:
1. Read the CSV file.
2. Create a bar chart using
plt.bar().
3. Use red bars.
4. Add the title "Monthly
Expenses".
5. Label both axes.
6. Add a grid.
7. Display the chart.
Q10. FAVOURITE SPORTS
File: sports.csv
Sport,Students
Football,40
Cricket,30
Basketball,20
Volleyball,15
Badminton,25
Write a Python program to:
1. Read the CSV file.
2. Create a bar chart using
plt.bar().
3. Use purple bars.
4. Add the title "Favourite
Sports".
5. Label both axes.
6. Display the chart.
========================================
SECTION C: PIE CHART (Q11–Q15)
========================================
Q11. MONTHLY EXPENSES DISTRIBUTION
File: expenses.csv
Category,Amount
Food,12000
Rent,15000
Education,8000
Transport,3000
Others,2000
Write a Python program to:
1. Read the CSV file using Pandas.
2. Create a pie chart using
plt.pie().
3. Display category labels.
4. Display percentage values using
autopct.
5. Add the title "Monthly
Expenses Distribution".
6. Display the chart.
Q12. SPORTS PARTICIPATION
File: sports.csv
Sport,Students
Football,40
Cricket,30
Basketball,20
Volleyball,10
Write a Python program to:
1. Read the CSV file.
2. Create a pie chart using
plt.pie().
3. Display sport names and
percentage values.
4. Set startangle=90.
5. Add the title "Sports
Participation".
6. Display the chart.
Q13. LIBRARY BOOK DISTRIBUTION
File: library.csv
Category,Books
Science,40
Fiction,30
History,15
Computer,10
Others,5
Write a Python program to:
1. Read the CSV file.
2. Create a pie chart using
plt.pie().
3. Display category labels.
4. Display percentage values up to
one decimal place.
5. Add the title "Library Book
Distribution".
6. Display the chart.
Q14. STUDENTS' MODES OF
TRANSPORTATION
File: transport.csv
Transport,Students
Bus,45
Bicycle,20
Walking,15
Motorcycle,10
Car,10
Write a Python program to:
1. Read the CSV file.
2. Create a pie chart using
plt.pie().
3. Display transport names and
percentage values.
4. Add the title "Students'
Modes of Transportation".
5. Display the chart.
Q15. COMPUTER PARTS SALES
File: computer.csv
Part,Sales
Keyboard,25
Mouse,30
Monitor,20
Printer,15
Speaker,10
Write a Python program to:
1. Read the CSV file.
2. Create a pie chart using
plt.pie().
3. Display labels and percentage
values.
4. Set startangle=90.
5. Add the title "Computer
Parts Sales".
6. Display the chart.
========================================
SECTION D: SCATTER CHART (Q16–Q20)
========================================
Q16. STUDY HOURS VS MARKS
File: study.csv
Hours,Marks
1,40
2,50
3,60
4,75
5,90
Write a Python program to:
1. Read the CSV file using Pandas.
2. Create a scatter chart using
plt.scatter().
3. Use red points with s=100.
4. Add the title "Study Hours
vs Marks".
5. Label both axes.
6. Add a grid.
7. Display the chart.
Q17. TEMPERATURE VS ICE CREAM SALES
File: temperature.csv
Temperature,Sales
20,30
25,45
30,60
35,80
40,100
Write a Python program to:
1. Read the CSV file.
2. Create a scatter chart using
plt.scatter().
3. Use blue points with s=80.
4. Add the title "Temperature
vs Ice Cream Sales".
5. Label both axes.
6. Add a grid.
7. Display the chart.
Q18. ADVERTISING VS SALES
File: advertising.csv
Advertising,Sales
1000,5000
2000,7000
3000,9000
4000,12000
5000,15000
Write a Python program to:
1. Read the CSV file.
2. Create a scatter chart using
plt.scatter().
3. Use green points with s=120.
4. Add the title "Advertising
vs Sales".
5. Label both axes.
6. Add a grid.
7. Display the chart.
Q19. AGE VS SALARY
File: salary.csv
Age,Salary
22,20000
25,25000
30,35000
35,45000
40,55000
Write a Python program to:
1. Read the CSV file.
2. Create a scatter chart using
plt.scatter().
3. Use purple points with s=100.
4. Add the title "Age vs
Salary".
5. Label both axes.
6. Add a grid.
7. Display the chart.
Q20. EXERCISE HOURS VS CALORIES
BURNED
File: exercise.csv
Hours,Calories
1,150
2,250
3,350
4,450
5,600
Write a Python program to:
1. Read the CSV file.
2. Create a scatter chart using
plt.scatter().
3. Use orange points with s=150.
4. Add the title "Exercise
Hours vs Calories Burned".
5. Label both axes.
6. Add a grid.
7. Display the chart.
========================================
END OF 20 PROGRAMMING QUESTIONS
========================================
Python programs | Matplotlib,
Seaborn, Plotly, Pandas and CSV
1. Write a Python
program to draw a vertical bar chart showing the marks obtained by five
students using Matplotlib. Give a suitable title and label both axes.
import matplotlib.pyplot as plt
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
plt.bar(students, marks,
color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Marks Obtained by
Students')
plt.show()
2. Write a Python program to draw a
horizontal bar chart showing the marks obtained by five students using
Matplotlib.
import matplotlib.pyplot as plt
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
plt.barh(students, marks,
color='green')
plt.xlabel('Marks')
plt.ylabel('Students')
plt.title('Marks Obtained by
Students')
plt.show()
3. Write a Python program to
display the marks obtained by five students using a Seaborn bar chart.
import seaborn as sns
import matplotlib.pyplot as plt
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
sns.barplot(x=students, y=marks,
color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Marks Obtained by
Students')
plt.show()
4. Write a Python program to draw
an interactive bar chart showing the marks obtained by five students using
Plotly Express.
import plotly.express as px
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
fig = px.bar(x=students, y=marks, title='Marks
Obtained by Students', labels={'x': 'Students', 'y': 'Marks'})
fig.show()
5. Write a Python program to create
a Pandas DataFrame containing student names and marks and draw a vertical bar
chart using Matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Name': ['Ram', 'Sita', 'Hari', 'Gita', 'Rita'],
'Marks': [85, 90, 75, 95, 80]
}
df = pd.DataFrame(data)
plt.bar(df['Name'], df['Marks'],
color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Marks Obtained by
Students')
plt.show()
6. Write a Python program to create
a Pandas DataFrame containing student names and marks and draw a bar chart
using Seaborn.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'Name': ['Ram', 'Sita', 'Hari', 'Gita', 'Rita'],
'Marks': [85, 90, 75, 95, 80]
}
df = pd.DataFrame(data)
sns.barplot(data=df, x='Name',
y='Marks', color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Marks Obtained by
Students')
plt.show()
7. Write a Python program to create
a Pandas DataFrame containing student names and marks and display an
interactive bar chart using Plotly Express.
import pandas as pd
import plotly.express as px
data = {
'Name': ['Ram', 'Sita', 'Hari', 'Gita', 'Rita'],
'Marks': [85, 90, 75, 95, 80]
}
df = pd.DataFrame(data)
fig = px.bar(df, x='Name',
y='Marks', title='Marks Obtained by Students')
fig.show()
8. Write a Python program to read
student names and marks from a CSV file using Pandas and draw a vertical bar
chart using Matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('marks.csv')
plt.bar(df['Name'], df['Marks'],
color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Student Marks from CSV')
plt.show()
9. Write a Python program to read
student marks from a CSV file using Pandas and draw a bar chart using Seaborn.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('marks.csv')
sns.barplot(data=df, x='Name',
y='Marks', color='skyblue')
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Student Marks from CSV')
plt.show()
10. Write a Python program to read
student marks from a CSV file using Pandas and create an interactive bar chart
using Plotly Express.
import pandas as pd
import plotly.express as px
df = pd.read_csv('marks.csv')
fig = px.bar(df, x='Name',
y='Marks', title='Student Marks from CSV')
fig.show()
11. Write a Python program to read
student marks from a CSV file using Pandas and draw a horizontal bar chart
using Matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('marks.csv')
plt.barh(df['Name'], df['Marks'],
color='green')
plt.xlabel('Marks')
plt.ylabel('Students')
plt.title('Student Marks from CSV')
plt.show()
12. Write a Python program to draw
a bar chart showing the marks of five students using different colors for each
bar.
import matplotlib.pyplot as plt
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
colors = ['red', 'blue', 'green',
'orange', 'purple']
plt.bar(students, marks,
color=colors)
plt.xlabel('Students')
plt.ylabel('Marks')
plt.title('Marks Obtained by
Students')
plt.show()
13. Write a Python program to draw
a vertical bar chart of five students' marks, including a suitable chart title,
X-axis label and Y-axis label.
import matplotlib.pyplot as plt
students = ['Ram', 'Sita', 'Hari',
'Gita', 'Rita']
marks = [85, 90, 75, 95, 80]
plt.bar(students, marks)
plt.title('Student Marks')
plt.xlabel('Student Names')
plt.ylabel('Obtained Marks')
plt.show()
14. Write a Python program to draw
a line chart showing the monthly sales of a shop for five months: January
(10000), February (15000), March (12000), April (18000) and May (22000). Use
Matplotlib and give suitable labels and a title.
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
plt.plot(months, sales)
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales')
plt.show()
15. Write a Python program to
display the monthly sales of a shop using a line chart with circular markers at
each data point. Use Matplotlib and add a title and axis labels.
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
plt.plot(months, sales, marker='o',
color='blue')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales with
Markers')
plt.show()
16. Write a Python program to draw
a line chart showing the monthly sales of a shop for five months using Seaborn.
Give a suitable title and label both axes.
import seaborn as sns
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
sns.lineplot(x=months, y=sales,
marker='o')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales Using
Seaborn')
plt.show()
17. Write a Python program to draw
an interactive line chart showing the monthly sales of a shop using Plotly
Express. Include a suitable title, labels and markers.
import plotly.express as px
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
fig = px.line(
x=months,
y=sales,
markers=True,
title='Monthly Sales',
labels={'x': 'Months', 'y': 'Sales (Rs.)'}
)
fig.show()
18. Write a Python program to
create a Pandas DataFrame containing monthly sales for five months and display
the data using a line chart in Matplotlib. Include a suitable title and labels.
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'Sales': [10000, 15000, 12000, 18000, 22000]
}
df = pd.DataFrame(data)
plt.plot(df['Month'], df['Sales'],
marker='o')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales Using
Pandas')
plt.show()
19. Write a Python program to
create a Pandas DataFrame containing months and sales and draw a line chart
using Seaborn. Give a suitable title and axis labels.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'Sales': [10000, 15000, 12000, 18000, 22000]
}
df = pd.DataFrame(data)
sns.lineplot(data=df, x='Month',
y='Sales', marker='o')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales Using
Seaborn')
plt.show()
20. Write a Python program to
create a Pandas DataFrame containing monthly sales and draw an interactive line
chart using Plotly Express. Display circular markers and add a suitable chart
title.
import pandas as pd
import plotly.express as px
data = {
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'Sales': [10000, 15000, 12000, 18000, 22000]
}
df = pd.DataFrame(data)
fig = px.line(
df,
x='Month',
y='Sales',
markers=True,
title='Monthly Sales Using Plotly',
labels={'Month': 'Months', 'Sales': 'Sales (Rs.)'}
)
fig.show()
21. A CSV file named sales.csv
contains the monthly sales of a shop. Write a Python program to read the CSV
file using Pandas and draw a line chart using Matplotlib. Give a suitable title
and label both axes.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('sales.csv')
plt.plot(df['Month'], df['Sales'],
marker='o')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales from CSV')
plt.show()
22. Write a Python program to read
monthly sales data from a CSV file named sales.csv using Pandas and display a
line chart using Seaborn. Include a suitable title and labels.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('sales.csv')
sns.lineplot(data=df, x='Month',
y='Sales', marker='o')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales from CSV')
plt.show()
23. Write a Python program to read
monthly sales from a CSV file named sales.csv using Pandas and draw an
interactive line chart using Plotly Express. Display markers and give a
suitable title and labels.
import pandas as pd
import plotly.express as px
df = pd.read_csv('sales.csv')
fig = px.line(df, x='Month', y='Sales',
markers=True, title='Monthly Sales from CSV', labels={'Month': 'Months',
'Sales': 'Sales (Rs.)'})
fig.show()
24. Write a Python program to read
the monthly sales of a shop from sales.csv using Pandas and draw a line chart
with circular markers, a blue line, a suitable title and axis labels using
Matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('sales.csv')
plt.plot(df['Month'], df['Sales'],
marker='o', linestyle='-', color='blue')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales with
Markers')
plt.show()
25. Write a Python program to draw
a line chart showing monthly sales of a shop using Matplotlib. Display a red
dashed line with circular markers. Give a suitable title and label both axes.
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
plt.plot(months, sales,
color='red', linestyle='--', marker='o'
)
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales')
plt.show()
26. Write a Python program to draw
a line chart representing the monthly sales of a shop for five months using
Matplotlib. Set the chart title to "Monthly Sales Report", the X-axis
label to "Months" and the Y-axis label to "Sales (Rs.)".
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar',
'Apr', 'May']
sales = [10000, 15000, 12000,
18000, 22000]
plt.plot(months, sales)
plt.title('Monthly Sales Report')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.show()
27. Write a Python program to draw
a pie chart showing the monthly household expenses of a family in five
categories: Food (Rs. 12000), Rent (Rs. 20000), Transport (Rs. 5000), Education
(Rs. 8000) and Others (Rs. 5000). Use Matplotlib, display category names and
add a suitable title.
import matplotlib.pyplot as plt
categories = ['Food', 'Rent',
'Transport', 'Education', 'Others']
expenses = [12000, 20000, 5000,
8000, 5000]
plt.pie(expenses,
labels=categories)
plt.title('Monthly Household
Expenses')
plt.show()
28. Write a Python
program to draw a pie chart showing the monthly expenses of a family using
Matplotlib. Display the percentage of each category up to one decimal place,
add labels and give a suitable title.
import matplotlib.pyplot as plt
categories = ['Food', 'Rent',
'Transport','Education', 'Others']
expenses = [12000, 20000, 5000,
8000, 5000]
plt.pie(expenses,
labels=categories, autopct='%1.1f%%')
plt.title('Monthly Household
Expenses')
plt.show()
29. Write a Python program to draw
a pie chart showing household expenses using Matplotlib with a Seaborn theme.
Display category labels, percentages and a suitable title.
import seaborn as sns
import matplotlib.pyplot as plt
categories = ['Food', 'Rent',
'Transport', 'Education', 'Others']
expenses = [12000, 20000, 5000,
8000, 5000]
plt.pie(expenses,
labels=categories, autopct='%1.1f%%')
plt.title('Monthly Household
Expenses')
plt.show()
30. Write a Python program to draw
an interactive pie chart showing the monthly household expenses of a family
using Plotly Express. Display category names and percentages with a suitable
title.
import plotly.express as px
categories = ['Food', 'Rent',
'Transport', 'Education', 'Others']
expenses = [12000, 20000, 5000,
8000, 5000]
fig = px.pie(names=categories,
values=expenses, title='Monthly Household Expenses')
fig.show()
31. Write a Python
program to create a Pandas DataFrame containing five household expense
categories and their amounts. Draw a pie chart using Matplotlib and display
category names, percentages and a suitable title.
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Category': ['Food', 'Rent', 'Transport',
'Education', 'Others'],
'Amount': [12000, 20000, 5000, 8000, 5000]
}
df = pd.DataFrame(data)
plt.pie(df['Amount'],
labels=df['Category'],
autopct='%1.1f%%')
plt.title('Monthly Household
Expenses')
plt.show()
32. Write a Python program to
create a Pandas DataFrame containing monthly household expenses and display
them using a pie chart with a Seaborn theme and Matplotlib.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'Category': ['Food', 'Rent', 'Transport',
'Education', 'Others'],
'Amount': [12000, 20000, 5000, 8000, 5000]
}
df = pd.DataFrame(data)
plt.pie(df['Amount'],
labels=df['Category'],
autopct='%1.1f%%')
plt.title('Monthly Household
Expenses')
plt.show()
33. Write a Python program to
create a Pandas DataFrame containing monthly household expenses and display an
interactive pie chart using Plotly Express. Add a suitable title and display
percentage values.
import pandas as pd
import plotly.express as px
data = {
'Category': ['Food', 'Rent', 'Transport',
'Education', 'Others'],
'Amount': [12000, 20000, 5000, 8000, 5000]
}
df = pd.DataFrame(data)
fig = px.pie(
df,
names='Category',
values='Amount',
title='Monthly Household Expenses'
)
fig.show()
34. A CSV file named expenses.csv
contains household expense categories and their amounts. Write a Python program
to read the CSV file using Pandas and draw a pie chart using Matplotlib.
Display category names, percentages and a suitable title.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('expenses.csv')
plt.pie(
df['Amount'],
labels=df['Category'],
autopct='%1.1f%%'
)
plt.title('Household Expenses from
CSV')
plt.show()
35. Write a Python program to read
household expenses from a CSV file using Pandas and draw a pie chart with a
Seaborn theme. Display category names, percentages and a suitable title.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('expenses.csv')
sns.set_theme(style='white')
plt.pie(
df['Amount'],
labels=df['Category'],
autopct='%1.1f%%'
)
plt.title('Household Expenses from
CSV')
plt.show()
36. Write a Python program to read
the expenses.csv file using Pandas and create an interactive pie chart using
Plotly Express. Display labels and percentages and give a suitable title.
import pandas as pd
import plotly.express as px
df = pd.read_csv('expenses.csv')
fig = px.pie(
df,
names='Category',
values='Amount',
title='Household Expenses from CSV'
)
fig.show()
37. Write a Python program to draw
a pie chart showing household expenses using Matplotlib. Use different colors
for each slice and display category names, percentages and a suitable title.
import matplotlib.pyplot as plt
categories = ['Food', 'Rent',
'Transport', 'Education', 'Others']
expenses = [12000, 20000, 5000,
8000, 5000]
colors = ['red', 'blue', 'green',
'orange', 'purple']
plt.pie(
expenses,
labels=categories,
colors=colors,
autopct='%1.1f%%'
)
plt.title('Monthly Household
Expenses')
plt.show()
37. Write a Python program to draw
a scatter plot showing the relationship between study hours and marks obtained
by five students using Matplotlib. Give a suitable title and label both axes.
|
Study Hours
|
Marks
|
|
1
|
40
|
|
2
|
50
|
|
3
|
65
|
|
4
|
75
|
|
5
|
90
|
import matplotlib.pyplot as plt
hours = [1, 2, 3, 4, 5]
marks = [40, 50, 65, 75, 90]
plt.scatter(hours, marks,
color='blue')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
38. Write a Python program to draw
a scatter plot showing the relationship between study hours and marks obtained
by five students using Seaborn. Add a title and appropriate axis labels.
import seaborn as sns
import matplotlib.pyplot as plt
hours = [1, 2, 3, 4, 5]
marks = [40, 50, 65, 75, 90]
sns.scatterplot(x=hours, y=marks,
color='green')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
40. Write a Python program to draw
an interactive scatter plot showing the relationship between study hours and
marks using Plotly Express. Include a suitable title and labels.
import plotly.express as px
hours = [1, 2, 3, 4, 5]
marks = [40, 50, 65, 75, 90]
fig = px.scatter(
x=hours,
y=marks,
title='Study Hours vs Marks',
labels={'x': 'Study Hours', 'y': 'Marks'}
)
fig.show()
41. Write a Python program to
create a Pandas DataFrame containing the study hours and marks obtained by five
students. Draw a scatter plot using Matplotlib with suitable labels and a
title.
import pandas as pd
import matplotlib.pyplot as plt
data = {
'Hours': [1, 2, 3, 4, 5],
'Marks': [40, 50, 65, 75, 90]
}
df = pd.DataFrame(data)
plt.scatter(df['Hours'],
df['Marks'], color='red')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
42. A CSV file named students.csv
contains the study hours and marks obtained by five students. Write a Python
program to read the CSV file using Pandas and draw a scatter plot using
Matplotlib. Give a suitable title and label both axes.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('students.csv')
plt.scatter(df['Hours'],
df['Marks'], color='blue')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
43. Write a Python program to draw
a scatter plot showing the relationship between study hours and marks obtained
by five students using Matplotlib. Use different colors and marker sizes for
each point. Give a suitable title and label both axes.
import matplotlib.pyplot as plt
hours = [1, 2, 3, 4, 5]
marks = [40, 50, 65, 75, 90]
sizes = [100, 200, 300, 400, 500]
colors = ['red', 'blue', 'green',
'orange', 'purple']
plt.scatter(hours, marks, s=sizes,
c=colors)
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
44. Write a Python program to
create a Pandas DataFrame containing the study hours and marks obtained by five
students. Draw a scatter plot using Seaborn. Give a suitable title and label
both axes.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'Hours': [1, 2, 3, 4, 5],
'Marks': [40, 50, 65, 75, 90]
}
df = pd.DataFrame(data)
sns.scatterplot(data=df, x='Hours',
y='Marks', color='blue')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
45. A CSV file named students.csv
contains the study hours and marks obtained by five students. Write a Python
program to read the CSV file using Pandas and draw an interactive scatter plot
using Plotly Express. Add a suitable title and axis labels.
import pandas as pd
import plotly.express as px
df = pd.read_csv('students.csv')
fig = px.scatter(
df,
x='Hours',
y='Marks',
title='Study Hours vs Marks',
labels={'Hours': 'Study Hours', 'Marks': 'Marks'}
)
fig.show()
46. Write a Python program to read
study hours and marks from a CSV file named students.csv using Pandas and draw
a scatter plot using Seaborn. Give a suitable title and label both axes.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('students.csv')
sns.scatterplot(data=df, x='Hours',
y='Marks', color='green')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.show()
47. Write a Python program to draw
a scatter plot showing the relationship between study hours and marks obtained
by five students using Matplotlib. Add grid lines, a suitable title and labels
for both axes.
import matplotlib.pyplot as plt
hours = [1, 2, 3, 4, 5]
marks = [40, 50, 65, 75, 90]
plt.scatter(hours, marks,
color='blue')
plt.xlabel('Study Hours')
plt.ylabel('Marks')
plt.title('Study Hours vs Marks')
plt.grid(True)
plt.show()