UNIT3 (1)
UNIT3 (1)
plt.show()
Horizontal Bar Chart
import matplotlib.pyplot as plt
plt.ylabel('Pets')
plt.xlabel('Popularity')
plt.title('Popularity of pets in the neighbourhood')
plt.show()
Comparing two data series
import numpy as np
import matplotlib.pyplot as plt
# data to plot
marks_john = [90, 55, 40, 65]
marks_sam = [85, 62, 54, 20]
# create plot
fig, ax = plt.subplots()
bar_width = 0.35
X = np.arange(4)
p1 = plt.bar(X, marks_john, bar_width, color='b',label='John')
# The bar of second plot starts where the first bar ends
p2 = plt.bar(X + bar_width, marks_sam, bar_width,color='g',label='Sam')
plt.xlabel('Subject')
plt.ylabel('Scores')
plt.title('Scores in each subject')
plt.xticks(X + (bar_width/2) , ("English", "Science","Sports", "History"))
plt.legend()
plt.tight_layout()
plt.show()
Histogram
ax.set_title('Survery responses')
plt.show()
Rotating Pie Chart
import matplotlib.pyplot as plt
x = [15, 25, 25, 30, 5]
labels = ['Very Likely', 'Likely', 'Unsure', 'Unlikely', 'Very Unlikely']
colors = ['tab:blue', 'tab:cyan', 'tab:gray', 'tab:orange', 'tab:red']
explode = [0, 0, 0, 0, 0.2]
fig, ax = plt.subplots()
ax.pie(x, labels = labels,
colors = colors,
autopct='%.0f%%',
explode = explode,
shadow = True,
startangle = 180)
ax.set_title('Survery responses')
plt.show()
Customizing Plot Legends