In today’s blog post, we’ll explore the concept of “Tiles” under the broader category of “Timeseries.” I wanted to revisit the usage of geom_tiles, which often creates heatmap-like charts. One interesting aspect of heatmaps is that they provide valuable insights, especially when one of the axes is ordinal, and even more so if it’s a time axis.
import polars as plimport polars.selectors as csimport datetimefrom lets_plot import*LetsPlot.setup_html()# Data Cleaningraw = (pl.read_csv('Resale_Flat_prices.csv',ignore_errors=True) .with_columns( pl.col('month').str.to_date('%Y-%m').alias('date'), pl.col('month').str.to_date('%Y-%m').dt.year().alias('year'), ))df = (raw .group_by(['month','date','town']) .agg(pl.col('resale_price').mean()) .sort(by=['date','town'], descending=[False, True]))(ggplot(df, aes(x='date',y='town',fill='resale_price'))+geom_tile(size=0.8)+scale_x_discrete( labels=[str(date.year) if date.month ==1else" "for date in df["date"].unique().to_list()] )+labs( x="", y="", title="The rise of Singapore Housing's Average Resale Prices", subtitle="From 2017 to 2024, across major neighbourhoods", caption ='#30DayChartChallenge #Day23 Tiles\nData: data.gov.sg\nMade by: www.ddanieltan.com', fill='Resale Price (SGD)' )+ theme( axis_text_x = element_text(angle=1, size=10), axis_text_y = element_text(size=5), legend_position ='right', legend_text= element_text(size=8), legend_title= element_text(size=10), plot_subtitle = element_text(size=10), plot_caption=element_text(size=12, color='grey'), )+ scale_fill_brewer(palette='YlGnBu')+ ggsize(1000,800))
TIL
In polars, when selecting a column using df.select(<col>) a Polars DataFrame is returned, and the DataFrame does not have a to_list() method. However, when selecting a column using df["<col>"] a Polars Series is returned that does have a to_list() method.
I faced a lot of difficulty in specifying which ticks I want to appear on the chart for my x axis of type datetime.date. My eventual solution seemed a little hacky but I’m satisfied I got close to what I wanted to visualise.