In this tutorial, I will show you how to draw point maps from point shapefile data.
The data is a set of major stations in Tokyo.
An administrative boundary (polygon) shapefile will be used as the background.
The most basic, most frequently used functions for mapping point shapefile is covered in this tutorial.
In [37]:
import geopandas as gpd # read and manage attribute table data
import matplotlib.pyplot as plt # prepare the figure
import colouringmap.mapping_point as mpoint # for drawing points
import colouringmap.mapping_polygon as mpoly # for mapping background polygon
import colouringmap.markerset as ms # getting more marker icons
from random import random # just for creating a random colour for demonstration
# the projection of the map, the data is in wgs84(epsg:4326), so need a proj dict for conversion
proj = {u'lon_0': 138, u'ellps': u'WGS84', u'y_0': 0, u'no_defs': True, u'proj': u'eqdc', u'x_0': 0, u'units': u'm', u'lat_2': 40, u'lat_1': 34, u'lat_0': 0}
## magic line for matplotlib
%matplotlib inline
the file contain some major railway stations.
read the shapefile and take a look.
In [53]:
stations = gpd.read_file('data/tweets_hotspot_station.shp')
In [54]:
stations.head()
Out[54]:
In [55]:
stations.crs
Out[55]:
Tthe projection of the file is epsg:4326, which is the latitude and longitude. So, lets project them to a projected crs.
In [56]:
stations = stations.to_crs(proj)
And now read the borders file, and do the projection.
In [5]:
borders = gpd.read_file('data/tokyo_special_ward.shp')
borders.head()
Out[5]:
In [6]:
borders = borders.to_crs(proj) # convert the borders projection to the same as the stations
print borders.crs==stations.crs # now check again if the two shapefile have the same projection
Now they are projected and have the same projection.
Lets, prepare the map with the borders file as a background: use mpoly.map_shape.
In [7]:
fig,ax = plt.subplots(figsize=(7,7))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
And, first try: map them using default settings with mpoint.map_scatter, which default to dot (marker='.')
In [8]:
fig,ax = plt.subplots(figsize=(7,7))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_scatter(stations, ax, extend_context=False)
Try to change the marker with a circle (marker='o'), and change to red colour.
In [9]:
fig,ax = plt.subplots(figsize=(7,7))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_scatter(stations, ax, extend_context=False,
marker='o', size=36, facecolor='red', alpha=.7)
And, change to square.
In [10]:
fig,ax = plt.subplots(figsize=(7,7))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_scatter(stations, ax, extend_context=False,
marker='s', size=36, facecolor='red', alpha=.7)
colouringmap package has included some font icons, which included maki (by mapbox) and so on...
to get a list of the icon_sets:
In [11]:
print ms.list_icon_sets()
to get a list of the icon in an icon set:
In [12]:
print ms.list_icon_names('maki')
print ms.list_icon_names('linecons')
to get an icon from the sets and names:
In [13]:
rail_icon = ms.get_marker('maki', 'rail')
shop_icon = ms.get_marker('linecons', 'shop')
The above xxx_icon will be used as the marker for mapping.
You can also use ms.show_icon() to take a look at the chosen icon.
In [19]:
ms.show_icon(shop_icon, size=36, face_colour='green')
Out[19]:
In [17]:
ms.show_icon(rail_icon, size=48)
Out[17]:
just similar with the previous map_scatter, but change the marker to the show_icon/rail_icon.
In [23]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_scatter(stations, ax, extend_context=False,
marker=shop_icon, size=12, facecolor='red', alpha=.9)
In [28]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_scatter(stations, ax, extend_context=False,
marker=rail_icon, size=24, facecolor='#4b0101', alpha=.9)
Sometimes, there are different types of points in a shapefile that you want to draw with different marker shapes, for differentiating them.
One way to do this, is create a temporally geodataframe, with distincted category, and map them with two lines of codes.
colouringmap provided another function named mpoint.map_category, which do the above procedures automatically for you. What you need to do is, provide a list of categories that you want to map (cat_order), and their style (marker_order, size_order, colour_order) in the same sequence.
In [29]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_category(stations,'Company', ax, size=48, extend_context=False)
By default, map_category function will use some default markers for the different categories.
The following show how to use different sets of styles for each category.
In [58]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_category(stations,'Company', ax, size=28, extend_context=False,
cat_order=['Tokyo Metro', 'Toei'], # category order
marker_order=[shop_icon, rail_icon],
size_order=[24,30],
colour_order=['r', 'g'])
The following demonstrate how to use map_colour to change the colour of the points according to a column that contain the colour info.
In this tutorial, I will just create some random colour for each point. The matplotlib rgb color is a set of three 0.0-1.0 float numbers.
In [38]:
col_list = []
for i in range(len(stations)):
r = random()
g = random()
b = random()
col_list.append((r,g,b))
stations['color'] = col_list
Now, map the points using the 'color' column.
In [39]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_colour(stations, 'color', ax, extend_context=False,
marker=rail_icon, size=24,alpha=.9)
The following tutorial show how to specify the size for each point, using a column.
The numbers in the column is intended to be used directly as a size. Well, this can also use as the proportional size for a variable.
To make a map with points using sizes that represent the breaking level, see another tutorial (part 2).
Let say we want to change the size of the points according to a column named 'DistanceBe'.
In [50]:
stemp = stations['DistanceBe'].tolist()
stemp2 = [ float(s)*20 for s in stemp ]
stations['size2'] = stemp2
stations['DistanceBe2'] = [ float(s) for s in stemp ]
In [52]:
stations.head()
Out[52]:
Now change the size according to the column "size2"
In [48]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_size(stations, 'size2', ax, extend_context=False,
marker=rail_icon, facecolor='green', alpha=.9)
Actually, this can also be done by using size_scale=20. (default to 1.).
But, note that the column is 'DistanceBe2', which is a column of numberic numbers, unlike the original column which is a string.
In [51]:
fig,ax = plt.subplots(figsize=(12,12))
ax = mpoint.prepare_map(ax, map_context=borders, background_colour='grey')
ax = mpoly.map_shape(borders, ax, lw=.1, alpha=.7, fc='#c1c6fc')
ax = mpoint.map_size(stations, 'DistanceBe2', ax, extend_context=False, size_scale=20.,
marker=rail_icon, facecolor='green', alpha=.9)
This is the end of the tutorial.
Most of the frequently used functions for mapping points were covered.
There are another tutorial (part 2) is for mapping the sequence, that include breaking,colouring,sizing of the points automatically according to a column of sequences.
In [ ]:
In [ ]: