I'm drawing a map of Hong Kong and I want to plot all the subway stations on the map. After specifying the bounding box, the center point in the Basemap function and converting the shapefile to the coordinate system 4326 using arcmap, I want to combine the Basemap and the shapefile together. But they don't match very well.
The bounding box I set is:
llcrnrlon=113.80779, llcrnrlat= 22.16694, urcrnrlon=114.416158, urcrnrlat=22.5783
And the center point is:
lat_0=22.394400, lon_0=114.156489
But what I get is the following plot:
The green points represent the subway stations in Hong Kong and the size of the point denote the number of tweets posted near each station. A larger point means more tweets are posted near this station.
From the plot, we could clearly see that the coastlines don't match the shapefile at all. Is it because of the quality of the Basemap function? The following shows all the codes I am using:
import matplotlib.pyplot as plt
import matplotlib.cm
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.colors import Normalize
dataframe = pd.read_csv(os.path.join(path, 'map_for_positive_negative.csv'))
dataframe['pos'] = dataframe.apply(lambda row: (row['lat'], row['lon']), axis=1)
# Plot the subway station on the map
def plot_station(pos):
count = dataframe.loc[dataframe.pos == pos]['Tweet Activity']
# x is longitude, y is latitude
x, y = m(pos[1], pos[0])
size = np.log2(count)
m.plot(x, y, 'o', markersize=size, color='#0FE500', alpha=0.8)
fig, ax = plt.subplots(figsize=(20, 20))
m = Basemap(resolution='f', # c, l, i, h, f or None
projection='mill',
lat_0=22.394400, lon_0=114.156489,
llcrnrlon=113.80779, llcrnrlat= 22.16694, urcrnrlon=114.416158, urcrnrlat=22.5783)
m.drawmapboundary(fill_color='#46bcec')
m.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
m.drawcoastlines()
# The coordinate system of the shapefile should be 4326. Use arcmap to change it
shape_file_path = r'...\hk_tpu'
m.readshapefile(shapefile=shape_file_path, name='hk_tpu')
dataframe.pos.apply(plot_station)
plt.show(m)
The file which records the location of the subway station is in here:
Subway station location in Hong Kong
The shapefiles I am using are in here:
Thank you very much!