4.7 Introduction to Data
Visualization
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
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()
3. 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()
4. 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()
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 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()
7. 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()
8. 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()
9. 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, marker='o',
color='blue')
plt.xlabel('Months')
plt.ylabel('Sales (Rs.)')
plt.title('Monthly Sales with
Markers')
plt.show()
10. 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()
11. 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()
12. 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()
13. 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()
14. 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()
15. 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()
16. 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()
17. 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()
19. 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()
20. 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()
22. 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()
23. 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()
24. 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()
25. 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()
26. 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()
27. 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()
28. 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()
29. 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()
30. 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()
31. 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()
No comments:
Post a Comment