-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBar Chart Tutorial.py
162 lines (97 loc) · 3.06 KB
/
Bar Chart Tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from matplotlib import style
# In[2]:
time_data = {'facebook':120, 'youtube':90, 'instagram':40, 'snapchat':28}
social_media = list(time_data.keys())
time_spend = list(time_data.values())
# In[3]:
style.use('ggplot')
plt.figure(figsize=(5,5))
color = ['royalblue', 'red', 'purple', 'yellow']
plt.bar(social_media, time_spend, color=color, width=0.5)
plt.xlabel('Social Media')
plt.ylabel('Time in Minutes')
plt.title('Average time spend on social media')
# In[4]:
fb = (120, 90, 110, 100, 95)
yt = (80, 90, 75, 85, 95)
ig = (50, 40, 55, 35, 45)
sc = (20, 30, 35, 25, 28)
# In[5]:
xpos = np.arange(len(fb))
xpos
# In[6]:
style.use('ggplot')
plt.figure(figsize=(10,7))
barWidth = 0.2
plt.bar(xpos, fb, color='royalblue', width = barWidth, label='FB')
plt.bar(xpos+0.2, yt, color='red', width = barWidth, label='YT')
plt.bar(xpos+0.4, ig, color='purple', width = barWidth, label='IG')
plt.bar(xpos+0.6, sc, color='yellow', width = barWidth, label='SC')
plt.xticks(xpos+0.3, ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'))
plt.xlabel('Day')
plt.ylabel('Time spend in minutes')
plt.title('Time spend on social media')
plt.legend()
# In[7]:
fb = (120, 90, 110, 100, 95)
yt = (80, 90, 75, 85, 95)
ig = (50, 40, 55, 35, 45)
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
xpos = np.arange(len(x))
barWidth = 0.4
plt.figure(figsize=(10,7))
plt.bar(xpos, fb, color='royalblue', width= barWidth, label='FB')
plt.bar(xpos, yt, bottom=fb, color='red', width= barWidth, label='YT')
plt.bar(xpos, ig, bottom=yt, color='purple', width= barWidth, label='IG')
plt.xlabel('Day')
plt.ylabel('Time spend in minutes')
plt.title('Time spend on socia media')
plt.xticks(xpos, x)
plt.legend()
# In[9]:
import os
os.chdir(r'E:\The AI & DS Channel\35_Bar chart\working file\data')
# In[10]:
car_sales = pd.read_csv('Car_sales.csv')
car_sales.head()
# In[11]:
manufacturer = car_sales['Manufacturer']
sales = car_sales['Sales_in_thousands']
# In[12]:
fig = plt.figure(figsize=(15,10))
plt.bar(manufacturer, sales)
plt.xlabel('Car Manufacturer')
plt.ylabel('Sale in 1000s')
plt.title('Car Sales')
plt.show()
# In[13]:
fig = plt.figure(figsize=(15,10))
plt.barh(manufacturer, sales, color='purple')
plt.xlabel('Car Manufacturer')
plt.ylabel('Sale in 1000s')
plt.title('Car Sales')
plt.show()
# In[28]:
toyota_df = car_sales.groupby('Manufacturer').get_group('Toyota')
toyota_df
# In[29]:
model = toyota_df['Model']
fuel_capacity = toyota_df['Fuel_capacity']
fuel_efficiency = toyota_df['Fuel_efficiency']
# In[30]:
fig = plt.figure(figsize=(15,10))
plt.barh(model, fuel_capacity, color='blue', height=0.5, label='Fuel Capacity')
plt.barh(model, fuel_efficiency, left=fuel_capacity, color='red', height=0.5, label='Fuel Capacity')
plt.ylabel('Car Model')
plt.xlabel('Fuel Capacity and Fuel Efficiency')
plt.title('Fuel capacity & fuel efficiency of various car models')
plt.legend()
plt.show()
# In[ ]: