I am trying to create an hourly heatmap from within Python. A code example of this graph made in R is available here: r-graph-gallery.com/283-the-hourly-heatmap.html. It relies on ggplot2.
There also is a Python implementation of ggplot2 called plotnine: github.com/has2k1/plotnine
Anybody able to “translate” from R to Python?
Here's a simple recreation in plotnine
docs using a small dataset. Most of the geom_*
elements from ggplot2
are implemented (mostly with underscores replacing dots in names). If you want to maintain the R
flavor of directly calling the geom_*
functions, you can just change the first line to from plotnine import *
.
import plotnine as p9
import pandas as pd
import numpy as np
#%% Build dataset
df = pd.DataFrame({'Month':pd.Categorical(['Jan','Feb','Mar']*2*3*4,
categories=['Jan','Feb','Mar']),
'Year':([2004]*3+[2005]*3)*3*4,
'Hour Commencing':([1]*3*2+[2]*3*2+[3]*3*2)*4,
'Day':[j for i in range(1,5) for j in [i]*3*2*3]})
# Add the hourly temp as random values
df['Hourly Temps'] = np.random.randint(-10,40,size=df.shape[0])
#%% Build the plot
g = (p9.ggplot(df,p9.aes(x='Day',y='Hour Commencing',fill='Hourly Temps'))
+ p9.geom_tile(color='white',size=.1)
+ p9.facet_grid('Year~Month')
+ p9.labs(title='Hourly Temps - Station T0001')
+ p9.scale_fill_cmap('plasma')
+ p9.theme(legend_position = 'bottom',
plot_title = p9.element_text(size=14),
axis_text_y = p9.element_text(size=6)))
print(g)