DATA VISUALIZATION USING MATPLOTLIB
Dataset: score.csv
# line plot
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("C://Users/Nageshwara Rao/Desktop/9-data-visualization/score.csv")
print(data.head())
x = data['marks']
y = data['rank']
plt.plot(x, y, color='orange')
plt.show()
# line plot and scatter plot
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("C://Users/Nageshwara Rao/Desktop/9-data-visualization/score.csv")
print(data.head())
x = data['marks']
y = data['rank']
# draw both the plots
plt.plot(x, y, color='orange', label= 'line plot')
plt.scatter(x, y, color='blue', label='scatter plot')
plt.legend()
plt.xlabel('marks', color='red')
plt.ylabel('rank', color='red')
plt.title('MARKS VS RANKS', color='green')
plt.show()
# bar graph
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("C://Users/Nageshwara Rao/Desktop/9-data-visualization/score.csv")
x = data['marks']
y = data['rank']
# draw bar graph, use ‘barh’ for horizontal bar graph
plt.bar(x, y, color='orange', label= 'bar graph')
plt.xlabel('marks', color='red')
plt.ylabel('rank', color='red')
plt.title('MARKS VS RANKS', color='green')
plt.xlim(700, 1000)
plt.ylim(2000, 2100)
plt.show()
# pie chart
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("C://Users/Nageshwara Rao/Desktop/9-data-visualization/score.csv")
x = data['marks']
# how many got marks above 950
x1 = len(x[x>950])
# how many got marks from 850 to 950
x2 = len(x[(x>=850) & (x<=950)])
# how many got marks from 750 to 850
x3 = len(x[(x>=750) & (x<=850)])
print(x1, x2, x3)
slices = [x1,x2,x3]
cols = ["Yellow", "Red", "Green"]
lbls = ["Above 950", "850-950", "750-850"]
plt.pie(slices, colors= cols, labels=lbls, startangle=90,
shadow=True,
explode= (0, 0.2, 0),
autopct='%.1f%%'
)
plt.show()
# sub plots
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv("C://Users/Nageshwara Rao/Desktop/9-data-visualization/score.csv")
x = data['marks']
y = data['rank']
z = data['admit']
fig = plt.figure()
graph1 = fig.add_subplot(2,2, 1, facecolor= "yellow")
graph1.plot(x, y)
graph1.set_title('marks vs rank')
graph2 = fig.add_subplot(2,2, 2, facecolor= "cyan")
graph2.plot(x, z)
graph2.set_title('marks vs admit')
# highest marks
x = max(x)
# lowest rank
y = min(y)
# no. of people admitted
z = len(z[z==1])
print(x,y,z)
graph3 = fig.add_subplot(2,2, 3, facecolor= "lightgreen")
graph3.axis("equal")
graph3.pie([x, y, z], colors=["Yellow", "Red", "Green"])
graph3.set_title('marks-rank-admissions')
plt.show()
# histogram
import matplotlib.pyplot as plt
emp_ages = [22,45,30,60,60,56,60,45,43,43,50,40,34,33,25,19]
bins = [0,10,20,30,40,50,60,70]
# histtype= {‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’}
plt.hist(emp_ages, bins, histtype='bar', rwidth=0.8, color='cyan')
plt.xlabel('employee ages')
plt.ylabel('No. of employees')
plt.title('MICROSOFT INC.')
plt.show()