Heat Map Rendering ExampleΒΆ
Purpose: Sample code to render heatmap of points
Imports
In [1]:
from tracktable.domain.terrestrial import TrajectoryPointReader
from tracktable.render import mapmaker
from tracktable.render.histogram2d import render_histogram
from tracktable.render import maps
from tracktable.core import data_directory
from matplotlib import pyplot
import os.path
/home/docs/checkouts/readthedocs.org/user_builds/tracktable/conda/v1.5.0/lib/python3.8/site-packages/traitlets/traitlets.py:3030: FutureWarning: --rc={'figure.dpi': 96} for dict-traits is deprecated in traitlets 5.0. You can pass --rc <key=value> ... multiple times to add items to a dict.
warn(
First we set up our point source by reading points from a file. Then we dump the points to a list. We do not care about extra data in this example, so we leave all the column fields as default.
The data file we use here is bundled with Tracktable.
In [2]:
points = []
data_filename = os.path.join(data_directory(), 'SampleHeatmapPoints.csv')
with open(data_filename, 'r') as inFile:
reader = TrajectoryPointReader()
reader.input = inFile
reader.comment_character = '#'
reader.field_delimiter = ','
for point in reader:
points.append(point)
Now we generate a map and create a heatmap from the points we generated.
The type of map, colors, scaling can be customised depending the on the desired look and feel of the finished map.
In [3]:
# JUPYTER NOTE: Jupyter will show you the state of the figure when you exit
# the cell in which you created it. You cannot apply different effects in
# different cells as far as I know. To work around this, just put all your
# different things in functions, then call those functions one after another
# in a single cell.
# Set up the canvas and map projection
# Grab default bounding box
def get_bbox(area, domain):
coords = []
location = maps.CONVENIENCE_MAPS[area]
coords.append(location['min_corner'][0])
coords.append(location['min_corner'][1])
coords.append(location['max_corner'][0])
coords.append(location['max_corner'][1])
return mapmaker._make_bounding_box(coords, domain)
# 100 DPI * (8, 6) gives an 800x600-pixel image
figure = pyplot.figure(dpi=100, figsize=(8, 6))
(mymap, map_actors) = mapmaker.mapmaker(domain='terrestrial',
map_name='region:conus')
bbox = get_bbox('conus', 'terrestrial')
render_histogram(map_projection=mymap,
point_source=points, # Our list of points we created above
bounding_box = bbox, # Bounding box is generated from mymap
bin_size=0.25,
colormap='gist_heat')
Out[3]:
[<matplotlib.collections.QuadMesh at 0x7f26c67fa0a0>]