A heatmap of daily temperatures

import numpy as np from PIL import Image, ImageDraw, ImageFont from matplotlib import cm from random import randint, seed seed(0) title = 'A heatmap of daily temperatures' # Load font font_path = '/usr/share/fonts/trutype/ttf-bitstream-vera/Vera.ttf' big_font = ImageFont.truetype(font_path, 24) small_font = ImageFont.truetype(font_path, 16) month_names = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ') lowest_temp, highest_temp = -15, 42 temp_range = highest_temp - lowest_temp months, max_days_in_month = 12, 31 month_days = [31, 30] * 6 month_days[1] = 28 observation_width, observation_height = 30, 30 left_months_offset, right_months_offset = 10, 50 top_title_offset, bottom_dates_offset = 50, 30 horizontal_offset = left_months_offset + right_months_offset vertical_offset = top_title_offset + bottom_dates_offset image_width = observation_width * max_days_in_month + horizontal_offset image_height = observation_height * months + vertical_offset im = Image.new('RGB', (image_width, image_height), (255, 255, 255)) draw = ImageDraw.Draw(im) color_map = cm.get_cmap('coolwarm') colors = [color_map(k/temp_range) for k in range(temp_range)] #print(colors) # Title draw.text((left_months_offset + image_width / 3.5, top_title_offset / 8), title, font=big_font, fill=0) # Month and day labels days_available = False for month_idx, days in enumerate(month_days): draw.text((left_months_offset, top_title_offset / 1.5 + month_idx * observation_height + observation_height/2), month_names[month_idx], font=small_font, fill=0) if not days_available: for k in range(days): draw.text((5*left_months_offset + k * observation_width + observation_width/4, top_title_offset + observation_height * months), str(k+1), font=small_font, fill=0) days_available = True # Heatmap imarr = np.array(im) for month_idx, days in enumerate(month_days): for k in range(days): startx = left_months_offset + (month_idx+1) * observation_width endx = left_months_offset + (month_idx+2) * observation_width starty = top_title_offset + k * observation_height endy = top_title_offset + (k+1) * observation_height # So nice for grayscale #normalized_temp = (randint(lowest_temp, highest_temp) - lowest_temp) / temp_range #imarr[startx:endx, starty:endy] = normalized_temp * 255 color = np.array(colors[randint(lowest_temp, highest_temp) - lowest_temp - 1]) imarr[startx:endx, starty:endy] = color[:3] * 255 im = Image.fromarray(imarr) im.save('heatmap.png') A heatmap of daily temperatures for all months in a year