Daylight diagram

Posted by – February 4, 2010

I tried to find a diagram with the distribution of daylight over the course of a year in Helsinki but couldn’t, so I made my own. Shockingly, my little Python script worked the first time I ran it:

The sudden jump in the line is due to daylight savings time.

Blue means night, orange means dawn/dusk, yellow means day. The left and right sides are midnight, the middle is midday. Top and bottom are New Year.

Here’s one with 06:00, 12:00 and 18:00 marked with a black line:

I can make bigger ones if someone wants them, or you can roll your own. I used a dataset I found on the web and wrote a Python script to generate the image. You need to have the Python Imaging Library installed for it to work.

# Generate daylight diagram. This produces quite a large file,
# size is controlled by pixels_per_min and day_width.
import Image, ImageFile, ImageDraw
from ImageColor import getrgb
data_file_name = "daylight-data.txt"
output_file_name = "daylight.png"
pixels_per_min = 1
day_width = 6
line_width = 2
light_color = getrgb("yellow")
dark_color = getrgb("midnightblue")
twilight_color = getrgb("orangered")
line_color = getrgb("black")
def h2min(s):
    hours, minutes = int(s.split(":")[0]), int(s.split(":")[1])
    return 60*hours + minutes
def min2px(flt):
    return int(round(flt*pixels_per_min))
# data file, formatted "date - sunrise - sunset - length of dawn+dusk"
f = open(data_file_name, "r")
data = [[h2min(line.split(" - ")[i]) for i in range(1,4)] for line in f.readlines()]
size = int(round(24*60*pixels_per_min)), len(data)*day_width
image ="RGB", size)
draw = ImageDraw.Draw(image)
for day in range(len(data)):
    y_top, y_bottom = day*day_width, (day + 1)*day_width
    twilightmargin = data[day][2]*0.25
    daystart = 0
    darkstop = dawnstart = min2px(data[day][0] - twilightmargin)
    dawnstop = lightstart = min2px(data[day][0] + twilightmargin)
    lightstop = duskstart = min2px(data[day][1] - twilightmargin)
    duskstop = darkstart = min2px(data[day][1] + twilightmargin)
    midnight = size[0]
    def fill(left, right, color):
        draw.rectangle([(left, y_top), (right, y_bottom)], color)
    fill(daystart, darkstop, dark_color)
    fill(dawnstart, dawnstop, twilight_color)
    fill(lightstart, lightstop, light_color)
    fill(duskstart, duskstop, twilight_color)
    fill(darkstart, midnight, dark_color)
def vline(percentage):
    pos = int(round(size[0]*percentage))
    draw.line([(pos, 0), (pos, size[1])], line_color, line_width)
# uncomment to draw vertical lines at 06:00, 12:00 and 18:00

edit: I made one with more markers so it’s more useful but less pretty:

6 Comments on Daylight diagram

Respond | Trackback

  1. veikko says:

    aika rivo kuva.


  2. janka says:

    Nice diagrams.

    I have found one online daylight graph for Helsinki here:


  3. Sampo Smolander says:

    CSC on tehnyt noita jo pitkään:


  4. sam says:

    pah :(


  5. sam says:

    On kyl todella hieno toi CSCn!