-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotChartExamples.py
More file actions
359 lines (237 loc) · 8.82 KB
/
Copy pathplotChartExamples.py
File metadata and controls
359 lines (237 loc) · 8.82 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from matplotlib import pyplot as plt
from collections import Counter
import numpy as np
import pandas as pd
import csv
# Display graph line
def showLine():
# Change the style
#print(plt.style.available)
plt.style.use('fivethirtyeight')
# Get values and plot
x_values = [24,25,26,27,28]
y1_data = [40, 32, 100, 98, 55]
plt.plot(x_values,y1_data, color='#5a7d9a', linestyle='--', marker='.', label='Female')
x2_values = [24,25,26,27,28]
y2_data = [140, 132, 100, 198, 155]
plt.plot(x2_values,y2_data, color='#adad3b', linestyle='-', linewidth=2, marker='o', label='Male')
#plt.bar(x2_values,y2_data, color='#adad3b')
# Set chart title and label axes and legend
plt.title('Median Salary by Ages')
plt.ylabel('Median Salary')
plt.xlabel('Ages')
plt.legend()
# Add a grid
plt.grid(True)
# Adjust and show plot
plt.tight_layout()
plt.show()
# Save to a file
plt.savefig('plot.png')
# Display graph bar
def showBar():
ages_x = [24,25,26,27,28]
x_indexes = np.arange(len(ages_x))
width = 0.25
y1_data = [40, 32, 100, 98, 55]
plt.bar(x_indexes,y1_data, width=width, label='Female')
y2_data = [140, 132, 100, 198, 155]
plt.bar(x_indexes - width, y2_data, width=width, label='Male')
# Adjust and show bar
plt.legend()
plt.xticks(ticks=x_indexes, labels=ages_x)
plt.title('Bar Chart Test')
plt.tight_layout()
plt.show()
def showByCSV():
plt.style.use('fivethirtyeight')
with open('data.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
language_counter= Counter()
for row in csv_reader:
language_counter.update(row['LanguagesWorkedWith'].split(';'))
# row = next(csv_reader)
# print (row['LanguagesWorkedWith'].split(';'))
# Print top 10
print (language_counter.most_common(10))
languages = []
popularity = []
# Get the X and Y axis
for item in language_counter.most_common(10):
languages.append(item[0])
popularity.append(item[1])
# print(languages)
# print(popularity)
# plot Horizontal bar chart
languages.reverse()
popularity.reverse()
plt.barh(languages, popularity)
# Set chart title and label axes and legend
plt.title('Top 10 popular languages')
plt.xlabel('Number of people who use')
plt.ylabel('Programming languages')
plt.legend()
plt.show()
def usePandaAndCSV():
data = pd.read_csv('data.csv')
# Put columns values into list
ids = data['Responder_id']
lang_responses = data['LanguagesWorkedWith']
# Intialise the Counter (that count the number of occurence within a list)
language_counter= Counter()
for response in lang_responses:
language_counter.update(response.split(';'))
languages = []
popularity = []
# Get the X and Y axis
for item in language_counter.most_common(10):
languages.append(item[0])
popularity.append(item[1])
# print(languages)
# print(popularity)
# plot Horizontal bar chart
languages.reverse()
popularity.reverse()
# Set chart title and label axes and legend
plt.title('Top 10 popular languages')
plt.xlabel('Number of people who use')
plt.tight_layout()
plt.barh(languages, popularity)
plt.show()
def pieChart():
slices = [2, 5, 7]
labels = [slices[0], slices[1], slices[2]]
colors = ['blue', 'red', 'green']
# Put the second value out of the pie chart out
explode = [0, 0.1, 0]
plt.pie(slices, labels=labels, explode=explode, colors=colors, wedgeprops={'edgecolor': 'black'}, shadow=True, autopct='%1.1f%%')
plt.title("Pie Chart")
plt.tight_layout()
plt.show()
def betterPieChart():
# Pie chart
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
# only "explode" the 2nd slice (i.e. 'Hogs')
explode = (0, 0.1, 0, 0)
#add colors
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%',
shadow=True, startangle=90)
# Equal aspect ratio ensures that pie is drawn as a circle
ax1.axis('equal')
plt.tight_layout()
plt.show()
def betterBarChart():
# set font
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['font.sans-serif'] = 'Helvetica'
# set the style of the axes and the text color
plt.rcParams['axes.edgecolor']='#333F4B'
plt.rcParams['axes.linewidth']=0.8
plt.rcParams['xtick.color']='#333F4B'
plt.rcParams['ytick.color']='#333F4B'
plt.rcParams['text.color']='#333F4B'
# create some fake data
percentages = pd.Series([20, 15, 18, 8, 6, 7, 10, 2, 10, 4],
index=['Rent', 'Transportation', 'Bills', 'Food',
'Travel', 'Entertainment', 'Health', 'Other', 'Clothes', 'Phone'])
df = pd.DataFrame({'percentage' : percentages})
df = df.sort_values(by='percentage')
# we first need a numeric placeholder for the y axis
my_range=list(range(1,len(df.index)+1))
fig, ax = plt.subplots(figsize=(5,3.5))
# create for each expense type an horizontal line that starts at x = 0 with the length
# represented by the specific expense percentage value.
plt.hlines(y=my_range, xmin=0, xmax=df['percentage'], color='#007ACC', alpha=0.2, linewidth=5)
# create for each expense type a dot at the level of the expense percentage value
plt.plot(df['percentage'], my_range, "o", markersize=5, color='#007ACC', alpha=0.6)
# set labels
ax.set_xlabel('Percentage', fontsize=15, fontweight='black', color = '#333F4B')
ax.set_ylabel('')
# set axis
ax.tick_params(axis='both', which='major', labelsize=12)
plt.yticks(my_range, df.index)
# add an horizonal label for the y axis
fig.text(-0.23, 0.96, 'Transaction Type', fontsize=15, fontweight='black', color = '#333F4B')
# change the style of the axis spines
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
# set the spines position
ax.spines['bottom'].set_position(('axes', -0.04))
ax.spines['left'].set_position(('axes', 0.015))
#plt.savefig('hist2.png', dpi=300, bbox_inches='tight')
plt.show()
def stackPlot():
plt.style.use('fivethirtyeight')
# Data
years = [2017, 2018, 2019, 2020]
server1 = [2, 2, 4, 5]
server2 = [2, 3, 4, 6]
server3 = [2, 4, 2, 10]
# Parameters for stack plot
labels = ['server1', 'server2', 'server3']
colors = ['#edf492','#efb960','#ee91bc']
# Initialise and display stack plot
plt.stackplot(years, server1, server2, server3, labels=labels, colors=colors)
#plt.legend(loc='upper left')
plt.legend(loc=(0.07,0.75))
plt.title("Stack Plot Example")
plt.tight_layout()
plt.show()
def fill_between():
data = pd.read_csv('data_fill.csv')
ages = data['Age']
dev_salaries = data['All_Devs']
py_salaries = data['Python']
js_salaries = data['JavaScript']
plt.plot(ages, dev_salaries, color='#444444',
linestyle='--', label='All Devs')
plt.plot(ages, py_salaries, label='Python')
overall_median = 57287
plt.fill_between(ages, py_salaries, dev_salaries,
where=(py_salaries > dev_salaries),
interpolate=True, alpha=0.25, label='Above Avg')
plt.fill_between(ages, py_salaries, dev_salaries,
where=(py_salaries <= dev_salaries),
interpolate=True, color='red', alpha=0.25, label='Below Avg')
plt.legend()
plt.title('Median Salary (USD) by Age')
plt.xlabel('Ages')
plt.ylabel('Median Salary (USD)')
plt.tight_layout()
plt.show()
def historgram():
# Define style
plt.style.use('Solarize_Light2')
# Read data from csv using panda dataFrame
data = pd.read_csv('data_Ages.csv')
ids = data['Responder_id']
ages = data['Age']
# Define the number of bins (number of histogram. Define the values in the axis)
bins = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
plt.hist(ages, bins=bins, edgecolor='black')
#plt.hist(ages, bins=bins, edgecolor='black', log=True)
median_age = 29
color = '#fc4f30'
plt.legend()
plt.axvline(median_age, color=color, label='Age Median', linewitdh=2)
plt.title('Age of the responders')
plt.tight_layout()
plt.show()
def howCounterWorks():
c = Counter(['Python', 'Java'])
print (c)
c.update(['Python', 'C++'])
print (c)
c.update(['Python', 'C++', 'Java'])
print (c)
#####################
# MAIN #
#####################
if __name__ == "__main__":
print(plt.style.available)
historgram()