1

I want to create a heatmap in Python that is similar to what is shown on the bottom of this screenshot from TomTom Move: https://d2altcye8lkl9f.cloudfront.net/2021/03/image-1.png (source: https://support.move.tomtom.com/ts-output-route-analysis/)

A route contains multiple segments that vary in length. Each segment consists of the average speed which I want to color using the colormap (green for fast speed to yellow to red for slow speed). I was able to plot each segment in their correct order using a stacked histplot, but when I add hue, it orders the segments with the fastest average speeds first to slowest, and not the segments in their correct order.

There are three time sets containing 4 segments with their length, length of the route so far and speed for each segment for each time set.

import pandas as pd 

d = {'timeRanges': ['00:00-06:30', '00:00-06:30', '00:00-06:30', '00:00-06:30', '07:00-15:00', '07:00-15:00', '07:00-15:00', '07:00-15:00', '16:00-17:30', '16:00-17:30', '16:00-17:30', '16:00-17:30'], 'segmentOrder': [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], 'segmentDistance': [20, 45, 60, 30, 20, 45, 60, 30, 20, 45, 60, 30], 'distanceAlongRoute': [20, 65, 125, 155, 20, 65, 125, 155, 20, 65, 125, 155], 'averageSpeed': [54.2, 48.1, 23.5, 33.7, 56.2, 53.2, 42.5, 44.2, 50.2, 46.2, 35.3, 33.2]}
df = pd.DataFrame(data=d)

I have tried using seaborn heatmap and imshow and I have yet to make the x axis block widths vary for each segment.

Much appreciated.

dyrjohn
  • 11
  • 2
  • If you use the `pcolormesh` from Matplotlib, you can specify each segment exactly how you want. See for example: https://stackoverflow.com/questions/17441914/matplotlib-matshow-how-to-change-each-row-height-based-on-a-scaling-vector – Rutger Kassies Sep 14 '21 at 07:23

1 Answers1

0

Here is a simple example of a heatmap with different box sizes. Based on the example "Heatmap with Unequal Block Sizes" https://plotly.com/python/heatmaps/. Just set the xe variable to all of the x-axis edges and z to the values that will be used for determining the colors between those points. There should be 1 fewer z value than xe value.

import plotly.graph_objects as go
import numpy as np

xe = [0, 1, 2, 5, 6]
ye = [0, 1]
z = [[1, 2, 1, 3]]


fig = go.Figure(data=go.Heatmap(
          x = np.sort(xe),
          y = np.sort(ye),
          z = z,
          type = 'heatmap',
          colorscale = 'Viridis'))

fig.update_layout(margin = dict(t=200,r=200,b=200,l=200),
    showlegend = False,
    width = 700, height = 500,
    autosize = False
                  )
fig.show()

enter image description here

Troy D
  • 2,093
  • 1
  • 14
  • 28