Class XII - Data Visualization (Slip Test) Solutions
Q1. Horizontal Bar Graph
import matplotlib.pyplot as p
Year = [2000, 2002, 2004, 2006]
Rate = [21.0, 20.7, 21.2, 21.6]
p.barh(Year, Rate) # To draw a line graph
p.xlabel('Year')
p.ylabel('Rate')
p.title('Fuel Rates in every Two Year')
p.savefig("Graph1.pdf") # To save the graph
p.show()
Q2. Pollution Index Bar Chart
import matplotlib.pyplot as plt
city = ["Delhi","Mumbai","Bangalore","Hyderabad"]
pollution = [23456123,20083104,18456123,13411093]
plt.bar(city, pollution, color='red')
plt.xlabel("City")
plt.ylabel("Pollution")
plt.title("Pollution Index")
plt.show()
Q3. Histogram
import matplotlib.pyplot as plt
data = [10,15,10,10,10,15,20,20,20,20,20,25,25]
plt.hist(data, bins=10, color='blue')
plt.xlabel("Score")
plt.ylabel("Frequency")
plt.title("Frequency of Score")
plt.show()
Q4. Weekly Attendance
import matplotlib.pyplot as pl
Days=['Mon', 'Tue', 'Wed', 'Thurs', 'Fri', 'Sat']
Present=[36, 37, 32, 31, 35, 39]
pl.bar(Days, Present) # Statement1
pl.show() # Statement2
Q5. Horizontal Bar Graph (Languages)
import matplotlib.pyplot as plt
x = ['Java', 'Python', 'PHP', 'JS', 'C#', 'C++']
popularity =[22.2, 17.6, 8.8, 8, 7.7, 6.7]
plt.barh(x, popularity, color="green") # Statement1
plt.xlabel("Popularity")
plt.ylabel("Languages")
plt.show()
Q6. Workshop Participants
import matplotlib.pyplot as plt
x = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN']
y = [30, 20, 30, 50, 10, 60]
plt.bar(x, y) # Statement 1
plt.xlabel("No. of Students attended") # Statement 2
plt.show()
Q7. Line Chart
import matplotlib.pyplot as plt
x = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN']
y = [30, 20, 30, 50, 10, 60]
plt.plot(x, y) # Statement 1
plt.title("Status of Workshop") # Statement 2
plt.show()
Q8. Subject Scores
import matplotlib.pyplot as plt
subjects = ['IP', 'Math', 'History']
scores = [91, 85, 78]
plt.barh(subjects, scores)
plt.title("Subject-wise Scores")
plt.xlabel("Score")
plt.ylabel("Subjects")
plt.show()
Q9. Error
Error: x and y lengths mismatch (x has 4 values, y has 3).
Q10. Correction
import matplotlib.pyplot as plt
subjects = ['IP', 'Math', 'English']
scores = [90, 85, 88]
plt.bar(subjects, scores)
plt.show()
Q11. Correction
import matplotlib.pyplot as plt
data = [12,15,13,17,19,21,22,22,23,25,28,29,30,31,32,35,36,37,38,40]
plt.hist(data, bins=8, color='skyblue', edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Sample Histogram')
plt.show()
Q12. Output
Bar chart with subjects Math, Science, History (Green bars, scores 85, 90, 80).
Q13. DataFrame
import pandas as pd
data = {'City': ['Mumbai', 'Delhi', 'Bengaluru'],
'Population': [20400000,16700000,8400000]}
df = pd.DataFrame(data)
print(df)
Q14. Line Graph
import matplotlib.pyplot as plt # Statement-1
students = ['John', 'Sarah', 'Mike', 'Emily']
scores = [88, 92, 75, 85]
plt.plot(students, scores, label='Test Scores') # Statement-2
plt.xlabel('Student Name')
plt.ylabel('Test Score') # Statement-3
plt.legend()
plt.title('Student Test Scores') # Statement-4
plt.show()
Q15. Lux Soap
import matplotlib.pyplot as plt
months = ['JAN', 'FEB', 'MARCH']
sales = [100, 78, 90]
plt.bar(months, sales)
plt.xlabel("Month")
plt.ylabel("Sales")
plt.title("Lux Soap")
plt.savefig("lux_sales.png")
plt.show()
Q16. Medal Comparison
import matplotlib.pyplot as plt # Statement1
X=["JAPAN","CHINA","INDIA","USA"]
Y=[45,91,6,126]
plt.xlabel("COUNTRIES")
plt.ylabel("TOTAL MEDALS") # Statement2
plt.bar(X,Y) # Statement3
plt.title("Medal Comparison") # Statement4
plt.show()
Q17. India Medal Tally
import matplotlib.pyplot as plt
medal_category=['Gold', 'Silver', 'Bronze']
medal=[20,15,18]
plt.bar(medal_category, medal, color='blue')
plt.xlabel("Medal Type")
plt.ylabel("Medal")
plt.title("Indian Medal tally in Olympics")
plt.savefig("aa.jpg")
plt.show()
Q18. Onion Price
import matplotlib.pyplot as plt # Statement-1
week=[1,2,3,4,5]
price=[25,30,40,60,20]
plt.bar(week, price, label='Onion') # Statement-2
plt.xlabel('Week')
plt.ylabel('Onion Price') # Statement-3
plt.legend()
plt.title("Weekly onion price chart") # Statement-4
plt.show()