TransWikia.com

Creating plots for each hour of each day with Python

Geographic Information Systems Asked by mankojag on February 16, 2021

I am trying to create an animated map (by generating multiple plots) of road traffic throughout a week, where the thickness of roads is represented by the volume of traffic at a specific time of day.

This is sort of what I’m looking for (but for each hour of each day):

enter image description here

The data has a structure that looks like this:

HMGNS_LNK_ID                 geometry    DOW  Hour   Normalised Value
         2     MULTILINESTRING ((251...    1     0   0.233623  
         2     MULTILINESTRING ((251...    1     1   0.136391
         2     MULTILINESTRING ((251...    1     2   0.108916

DOW stands for ‘day of the week’ (1 = Monday) and so for every Hour of each of the 7 days I want to plot the map with roads’ thickness by the value Normalised Value.

I encounter a problem that when trying to loop with this code:

for dow in df['DOW']:
    fig, ax = plt.subplots(1)
    day_df = df[df['DOW']==dow]

    for hour in day_df['Hour']:
        day_hour_df = day_df[day_df['Hour']==hour]
        day_hour_df.plot(ax=ax, linewidth=day_hour_df['Normalised Value'])
        plt.savefig("day{}_hour{}.png".format(dow, hour), dpi = 200, facecolor='#333333')

The problem is that the figures are saved only for day 1, so until day1_hour_23 and after that, it comes back to day1_hour0 and overwrites the plot with something new. I can’t figure out why it stops at DOW 2.

I’m not even sure if the data structure is correct. I would greatly appreciate any help with that. Please find the full code in my GitHub repository.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP