python: Creating a simple bar plot with python, matplotlib module and csv data

This is so simple, it needs no heading..

08/19/201810:10:49 PM Drinks Coffee as usual…

Straight to the point:

So this is the CSV data I will be working with ( generated using the website:


Goal: Create a bar plot showing  the number of emails based on the alphabets they begin with . For instance, how many emails begin with “a” etc… This can be useful for visualizing emails from various email providers (e.g. yahoo, gmail, etc..).

The code……..


<p>import csv, matplotlib.pyplot as plt, numpy as np<br />
filename = &quot;MOCK_DATA.csv&quot;</p>
<p>def count_email(rows):<br />
	dictionary = {}<br />
	for row in rows:<br />
		email = row[3]<br />
		if email[0] not in dictionary:<br />
			dictionary[email[0]] = 0<br />
		dictionary[email[0]] += 1<br />
	return dictionary;</p>
<p>def plot_hist(dictionary):<br />
	keys = dictionary.keys()<br />
	data = []<br />
	for key in keys:<br />
		data.append(dictionary[key])<br />
	x = np.arange(len(keys))<br />, height=data)<br />
	plt.xticks(x+0.1, keys)<br />
	plt.xlabel(&quot;Emails start with...&quot;)<br />
	plt.ylabel(&quot;Frequency&quot;)<br /></p>
<p>data = [] </p>
<p>try:<br />
	with open(filename) as f:<br />
		reader = csv.reader(f)<br />
		data = []<br />
		for row in reader:<br />
			data.append(row)<br />
except csv.Error as e:<br />
	print(&quot;Error while readinf csv file &quot;, e)<br />
	sys.exit(-1)<br />
if data:<br />
	soln = count_email(data)<br />
	if soln:<br />

Notes: This assumes your files are all in the same directory ( same level).

Running the code like this: 




Well that was it… if you have suggestions, improvements, questions, do not hesitate to comment down below.

See ya.