Simple Data Visualization Using Matplotlib

Nur Ahmed
3 min readJan 8, 2021

I like to track and measure my day to day activity and productivity. There is this quote I like -

'What gets measured , gets managed ' - Peter Drucker

I like clockify.me for this purpose. Because it is free for basic usage and very simple to use. You could use its timer or manually enter the duration, you could add tags and other metadata. It has some basic visualization and query features also. It has a web app, desktop app, and mobile app.

Now if you go to the dashboard and select your range of dates you get to see a bar chart like this —

Now, this chart shows values in hours and mins. I wanted to see a bar chart in mins only (Why? I don't know. I just liked to).

'Engineers like to solve problems. If there are no problems handily available, they will create their own problems.' - Scott Adams

But there is no built-in feature for this in there. But luckily you could download all your data as CSV in the ‘Report’ feature under the ‘Detailed’ section -

So I thought this might be a good chance to brush up on my python skills. Here I will discuss how I tackled this problem using python and matplotlib library.

Reading the CSV file

The CSV file looks like this

https://gist.github.com/mdnurahmed/cd65d2d141f3b29d0fe1a47f130a5484

I used the built-in CSV library to read the CSV file. For my purpose, I only need the 8th (dates) and 12th (durations) columns in this CSV file. .I can have a dictionary where the key is the date and the value is the total duration. I like to use defaultdict over dict as it can have a default value for a key and will not throw an exception if the key is not present in the dictionary.

https://gist.github.com/mdnurahmed/da9315bacfcea5330c6dd2c60a69b0b7

Sorting by date

Now I gotta sort the dictionary according to the dates. I could do that by converting the dictionary to a list first and then sorting that list. There is an easy way to convert a dictionary to a list of tuples -

>>> d1 = {'name': 'Ravi', 'age': 23, 'marks': 56}
>>> l1 = list(d1.items())
>>> l1
[('name', 'Ravi'), ('age', 23), ('marks', 56)]

Now to sort the list by the date we could use the datetime module. In Python, we have the datetime module which makes date based comparison easier. The datetime.strptime() function is used to convert a given string into datetime object. It accepts two arguments: date (string) and format (used to specify the format). We could use the datetime object as the key for sorting.

https://gist.github.com/mdnurahmed/899b2a192fa3c9825436fa132b11fdce

Generating bar chart using matplotlib

To generate a bar chart we could use the pyplot.bar() function in matplotlib module.

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

The first and second arguments are compulsory. The first argument is the coordinates of the bars, the second argument is the height of the bars .We could use the pyplot.xtick() function to give labels to the bars in the x-axis on their x-coordinate values.

matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)

We could use the pyplot.text() function to put values of bars as labels on top of the bars.

matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)[source]

The code should look something like this -

https://gist.github.com/mdnurahmed/83e9ab9bd72be766e4beb4af85aaed19

Here is the full code :

https://gist.github.com/mdnurahmed/0140bf7058e5d1b3aee7e03f724a307d

Here is the bar chart I got :

--

--