Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an activity plot from Pandas Dataframe (like the Github contribution plot)

You all know this graph:

enter image description here

There you have a day of the week and the month of a year with some kind of activity. Let's say I have this Pandas dataframe:

In [87]: metadf2[['Week','Activity']]
Out[87]:
     Week Activity
weekday     
0    15  1.6
0    15  1.1
0    17  0.6
0    17  0.8
0    17  1.3
0    17  2.6
0    17  0.9
0    19  1.0
0    19  8.0
0    19  1.6
0    23  5.0
0    23  1.2
0    23  0.6
0    23  5.6
1    15  1.6
1    15  0.2
1    15  0.1
1    15  0.1
1    15  0.4
1    17  12.2
1    19  10.2
1    19  1.6
2    13  1.7
2    14  0.0
2    14  0.0
2    15  6.9
2    15  2.5
2    15  5.5
2    17  6.2
2    17  1.3
... ... ...
3    14  1.1
3    14  4.9
3    14  4.0
3    14  1.5
3    14  3.9
4    14  0.2
5    15  5.4
5    15  5.1
5    18  9.5
5    18  8.8
5    20  108.8
5    20  11.1
5    20  11.2
6    13  74.9
6    13  2.0
6    13  3.2
6    13  2.0
6    13  16.7
6    13  5.5
6    16  0.4
6    15  7.6
6    15  11.7
6    15  25.8
6    16  0.4
6    16  0.4
6    16  1.3
6    20  2.0
6    20  20.5
6    20  77.0
6    20  32.8

How to create such a Github-Activity-like graph with Matplotlib? I think the contour plot with nearest is in the right direction, isn't it?

like image 673
Balzer82 Avatar asked Jun 11 '14 12:06

Balzer82


People also ask

How do you plot a DataFrame plot?

line() function is used to plot series or DataFrame as lines. This function is useful to plot lines using DataFrame's values as coordinates. Columns to use for the horizontal axis.

Can pandas generate graphics plots?

Initializing the Plots Object Plotting can be performed in pandas by using the “. plot()” function. This function directly creates the plot for the dataset. This function can also be used in two ways.

Can you plot pandas DataFrame with Matplotlib?

Matplotlib is an amazing python library which can be used to plot pandas dataframe.


Video Answer


1 Answers

Here is an example for you to start with:

import pylab as pl
import numpy as np
import pandas as pd

# prepare some random data
N = 100
np.random.seed(0)
weekday = np.random.randint(0, 7, N)
week = np.random.randint(0, 40, N)
activity = np.random.randint(0, 100, N)

df = pd.DataFrame({"weekday":weekday, "week":week, "activity":activity})
df.drop_duplicates(subset=["weekday", "week"], inplace=True)

# reshape the data and plot it
df2 = df.pivot(columns="week", index="weekday", values="activity")
df2.fillna(0, inplace=True)

Weekday, Week = np.mgrid[:df2.shape[0]+1, :df2.shape[1]+1]
fig, ax = pl.subplots(figsize=(12, 4))
ax.set_aspect("equal")
pl.pcolormesh(Week, Weekday, df2.values, cmap="Greens", edgecolor="w", vmin=-10, vmax=100)
pl.xlim(0, df2.shape[1])

the output:

enter image description here

like image 106
HYRY Avatar answered Sep 28 '22 04:09

HYRY