-1

I have a list of coordinates and timestamps spread over a 24-hour period. I want to create an animated heat map which will show the map throughout the day.

I've tried looking at Basemap's example gallery but the code seems a little too complex for me to come up with even a basic heat map. What I don't understand specifically is the data parameter of the contourf() function.

How do I proceed?

PS: Sample data:

[{'date': datetime.datetime(2013, 3, 12, 6, 13, 0), 'loc': (46.448269, 16.363296)},
 {'date': datetime.datetime(2013, 3, 12, 6, 13, 1), 'loc': (37.702152, -120.127964)},
 {'date': datetime.datetime(2013, 3, 12, 6, 13, 1), 'loc': (14.484318, 121.037639)},
 ...]
seriousdev
  • 7,519
  • 8
  • 45
  • 52

2 Answers2

2

data is the data you want the contours to represent. To draw contours, you are associating a value z with each (x, y) point on your map. data is z. For instance, if you wanted to make a contour map of temperatures, data would be the temperatures.

Given your sample data, I don't understand what you want to plot. You have timestamps and locations, but no data associated with those values. It doesn't make sense to plot a contour with just that (unless you have only one timestamp per location, and you want to plot a single, static map with the time as the z value). You can't just have "an animated contour map throughout the day", you need to have an animated contour map of something (i.e., some other quantity) throughout the day. What do you want the values displayed on the map to represent? What is your dependent variable?

If you want the variable to be "number of data points at these coordinates", then you should calculate that first. That is, group your data by coordinates/time and count the number of data points at each place/time. You want the equivalent of a table like:

X    Y   Time   Points
-10  2   10:30  4
-10  2   11:00  10
4    -10 10:30  6
4    -10 11:00  8

You can then generate a series of contour maps where your data variable is taken from the "Points" column. I'd recommend you look at the pandas library for convenient ways of slicing and aggregating the data (e.g., to group by coordinates and/or time).

An alternative possibility is to use the technique described here.

Community
  • 1
  • 1
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Well I want the places to be darker (or redder, or whatever you like) based on the number of points on those coordinates, like what [heatmap.py](http://jjguy.com/heatmap/) offers. – seriousdev Mar 16 '13 at 19:38
0

As the documentation suggest, try reading the explanation of matplotlib.pyplot.contourf for details on the data paramemter. The code look complicated just because it is loading geografical data from the net. and drawing both continents and seas and meridians.

I'm a little rusty in the Basemap usage, but the core function calls (given the example in the page) are:

# create the map, i.e. the plot surface as a map projection given
# the type of projection you need and the point you want as center of the map
map = Basemap(projection='ortho', lat_0=45, lon_0=-100, resolution='l')

# draw the continents lines
map.drawcountries(linewidth=0.25)

# draw the contour plot
cs = map.contour(x, y, data, 15, linewidths=1.5)

Guess it can't be much easier than that. If you nedd a more detailed solution, please share the details of your problem, the data structure that you have and some code that you have tried.

EnricoGiampieri
  • 5,947
  • 1
  • 27
  • 26
  • 1
    Yep, I already have something like that but I still have no clue on what to put inside `data` as the documentation isn't particularly explicit. See my edit for sample data. – seriousdev Mar 16 '13 at 14:37
  • What did you make of this? Have you found a solution? – FaCoffee Jul 23 '18 at 10:12