I'm trying to compare two sets of London Airbnb data. I want an elegant way to plot the London shapefile on two subplots, and then overlay the different data as points on each map. My shapefile is from here:
londonshp = gpd.read_file("statistical-gis-boundaries london\ESRI\London_Borough_Excluding_MHW.shp")
londonshp = londonshp.to_crs(4326)`
This is the code to plot the maps:
fig, axes = plt.subplots(ncols=2, figsize = (12,16))
#entire home/apt on left
axes[0].set_aspect('equal')
londonshp.plot(ax = axes[0],
color = '#e0e1dd',
edgecolor = '#1c1c1c')
axes[0].scatter(entirehomedf.longitude,
entirehomedf.latitude,
s = 1,
c = '#2ec4b6',
marker = '.')
axes[0].set_yticklabels([])
axes[0].set_xticklabels([])
axes[0].set_title("Entire Homes/Apts")
#private room on right
axes[1].set_aspect('equal')
londonshp.plot(ax = axes[1],
color = '#e0e1dd',
edgecolor = '#1c1c1c')
axes[1].scatter(privateroomdf.longitude,
privateroomdf.latitude,
s = 1,
c = '#ff9f1c')
axes[1].set_yticklabels([])
axes[1].set_xticklabels([])
axes[1].set_title("Private Rooms")
Result:
The code I have works fine, but it seems inelegant.
- Manually plotting the shapefile on each subplot is ok for just two subplots, but not ideal for larger numbers of subplots. I imagine there's a quicker way to do it automatically (e.g. a loop?)
- Some scatterplot features (like marker shape/size) are the same on each subplot. I'm sure there's a better way to set these features for the whole figure, and then edit features which are individual to each subplot (like colour) separately.
CodePudding user response:
I won't code it out, but I can give you some tips:
- Yes you can use loops to plot multiple subplots, all you have to do is iterate through multiple lists of variables you want to change e.g. colour and data and use them in the loop
- When you use the loop, you can easily access all the different variables needed, including all your features for your graphs, e.g.:
c= ["blue","red","yellow"]
for x in range(3):
plt.plot(...,color=c[x])