Making 3 Easy Maps With Python

We’ll be using a data set on all Starbucks locations in Los Angeles County for this tutorial.

By the end of this introductory post you will be able to create:A basic point map of all Starbucks locations in LA CountyA choropleth map which shades in zip codes in LA County based on how many Starbucks are contained in each oneA heatmap which highlights “hotspots” of Starbucks in LA CountyLet’s do it!You will need …The Python package pandas.

This is used to easily manipulate data in PythonThe Python package folium.

This is used to very easily create mapsA spreadsheet with latitude/longitude of Starbucks in LA County (on my GitHub here)A GeoJSON (basically a json which describes a complex shape) of LA County (here) and a GeoJSON of all the zip codes in LA County (here)To get familiar with the data, here’s a snapshot of the first few rows:We only need to worry about the latitude, longitude, and zip fields for this analysis.

Here are the needed Python imports, loading the Starbucks data, and loading the LA County GeoJSON:Basic Point MapCreating a basic point map of all Starbucks in LA County from the latitude/longitude pairs in our dataframe is pretty straightforward.

Opening up laPointMap.

html, we see the following map:We can clearly see all the Starbucks in LA County as little red dots within the LA County region.

Of course, you can customize any of the colors and shapes of the dots.

Choropleth MapI actually didn’t know what a choropleth map was before playing with maps in Python but it turns out they are very useful in visualizing aggregated geospatial data.

Our choropleth map will answer the question: “Which zip codes in LA County have the most Starbucks?”.

The choropleth map essentially colors in each zip code based on the value of some other variable, the number of Starbucks stores in our case.

Let’s first go over the basic code needed to create one:Since I’ve personally found it more difficult to understand how to get all the components in place for a choropleth, let’s take a look at a separate visual to see how it works.

The choropleth needs to know what color to fill in for zip code 90001, for example.

It checks the pandas dataframe referenced by the data field, searches the key_on column for the zip code and finds the other column listed in columns which is numStores.

It then knows that it needs to fill in the color corresponding to 3 stores in zip code 90001.

It then looks in the GeoJSON referenced by the geo_path field, and finds zip code 90001 and its associated shape info, which tells it which shape to draw for that zip code on the map.

Through these links, it has all the necessary information.

Let’s look at the resulting choropleth in laChoropleth.

html!We see that it comes with a nice color bar at the top for reference.

HeatmapIn the choropleth map above, we see that areas in south LA County seem to have more Starbucks stores in general, but can we get a bit more specific?.Can we maybe figure out where there are a lot of Starbucks stores in a small vicinity?.Basically, let’s create a heatmap to highlight Starbucks “hotspots” in LA County.

The main parameters in the heatmap that need some trial and error are radius which controls how big the circles are around each Starbucks store and blur which controls how much the circles “blend” together.

A higher radius means any given Starbucks influences a wider area and a higher blur means that two Starbucks which are further away from each other can still contribute to a hotspot.

The parameters are up to you!Let’s see a picture of our heatmap in laHeatmap.


Hmm … cool but it kind of seems like everything is red.

Heatmaps might be more valuable if you zoom in.

Let’s zoom in a bit and see if we can identify more specific hotspots.

Nice!.It’s pretty clear from the above map that we have some hotspots and some not-hotspots (notspots?) in the map.

One that stands out is in Downtown Los Angeles (understandably).

And that’s about it!.My only regret is that I haven’t yet found a way to embed the actual interactive versions of these maps in a Medium post so I was only able to show you screenshots.

I strongly encourage you to run the small bits of code through this post to play with the interactive maps for yourself.

It’s a totally different experience.

I hope this post helped a bit and I’ll see you in the next one!The full notebook containing all code used in this analysis can be found at my GitHub here.


. More details

Leave a Reply