Visual - Wrksht-2 (With Solutions)
Visual - Wrksht-2 (With Solutions)
Ans:
Q4. Draw the following bar graph representing the number of students in each class.
Q5. What changes do I make in the code of Q3 so that my output shows horizontal bars?
Ans: import matplotlib.pyplot as p
city=["Delhi","Mumbai","Bengaluru","Kolkata"]
accident=[60,40,55,30]
p.barh(city,accident, color="orange", edgecolor="black")
p.title('Accident Tally of 4 Metropolitan cities')
p.xlabel('Accidents')
p.ylabel('City')
p.show()
Q6. WAP to store the following data in a dataframe, making the city names as the index. And then
plot one bar graph to show the number of number of active cases and total cases for all cities and
another bar graph to show the active cases and the number of deaths.
Cities total active death
Maharashtra 15,00,000 45000 11,000
Karnataka 12,00,000 38000 8,000
Kerala 8,00,000 25000 10,000
Ans:
import matplotlib.pyplot as plt
import pandas as pd
dict1={'total':[1500000,1200000,800000],
'active':[45000,38000,25000],
'death':[11000,8000,10000]
}
df=pd.DataFrame(dict1, index=['Maharashtra','Karnataka','Kerala'])
df[['total','active']].plot(kind='bar', color=['green','black'])
plt.title("Comparison of Active corona cases across various cities",fontsize=14,color="blue")
plt.xlabel("City",fontsize=14,color="red")
plt.ylabel("Corona Cases",fontsize=14,color="red")
plt.xticks(fontsize=10, rotation=30)
plt.show()
df[['active','death']].plot(kind='bar', color=['red','yellow'])
plt.title("Comparison of deaths of corona cases across various cities",fontsize=14,color="blue")
plt.xlabel("City",fontsize=14,color="red")
plt.ylabel("Corona Cases",fontsize=14,color="red")
plt.xticks(fontsize=10, rotation=30)
plt.show()
Output 1:
Output 2:
Q7. Fill in the blank with the correct statement to plot a bar graph using a matplotlib method, so
that Company ABC can see the graphical presentation of its profit figures for the 2 nd quarter of the
financial year 2019 (ie, Aug, sep, Oct, Nov).
import matplotlib.pyplot as mtp
Months=[‘AUG’,’SEP’,’OCT’,’NOV’] #X Axis
Profits=[125,220,230,175] #Y Axis
_____________________________________________
mtp.show()
Ans 7.
import matplotlib.pyplot as mtp
Months=['AUG','SEP','OCT','NOV'] #X Axis
Profits=[125,220,230,175] #Y Axis
mtp.bar(Months,Profits)
mtp.show()