You can do nice visualizations on maps using the folium package. Install it using:
pip install folium
Import essential packages:
import pandas as pd
import folium
from matplotlib import colors as mcolors
import matplotlib.pyplot as plt
Load up data:
# World cities downloaded from https://simplemaps.com/data/world-cities
cities = pd.read_csv("worldcities.csv")
cities.head()
city | city_ascii | lat | lng | country | iso2 | iso3 | admin_name | capital | population | id | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | Malishevë | Malisheve | 42.4822 | 20.7458 | Kosovo | XK | XKS | Malishevë | admin | NaN | 1901597212 |
1 | Prizren | Prizren | 42.2139 | 20.7397 | Kosovo | XK | XKS | Prizren | admin | NaN | 1901360309 |
2 | Zubin Potok | Zubin Potok | 42.9144 | 20.6897 | Kosovo | XK | XKS | Zubin Potok | admin | NaN | 1901608808 |
3 | Kamenicë | Kamenice | 42.5781 | 21.5803 | Kosovo | XK | XKS | Kamenicë | admin | NaN | 1901851592 |
4 | Viti | Viti | 42.3214 | 21.3583 | Kosovo | XK | XKS | Viti | admin | NaN | 1901328795 |
Get the ten largest Swedish cities:
swedish_cities = cities[cities.country == "Sweden"]
swedish_cities.sort_values(by="population", ascending=False)[:10]
city | city_ascii | lat | lng | country | iso2 | iso3 | admin_name | capital | population | id | |
---|---|---|---|---|---|---|---|---|---|---|---|
2789 | Stockholm | Stockholm | 59.3508 | 18.0973 | Sweden | SE | SWE | Stockholm | primary | 1264000.0 | 1752425602 |
2780 | Göteborg | Goteborg | 57.7500 | 12.0000 | Sweden | SE | SWE | Västra Götaland | admin | 537797.0 | 1752059374 |
2786 | Malmö | Malmo | 55.5833 | 13.0333 | Sweden | SE | SWE | Skåne | admin | 269349.0 | 1752705818 |
2793 | Uppsala | Uppsala | 59.8601 | 17.6400 | Sweden | SE | SWE | Stockholm | NaN | 133117.0 | 1752330831 |
2774 | Västerås | Vasteraas | 59.6300 | 16.5400 | Sweden | SE | SWE | Västmanland | admin | 107194.0 | 1752826955 |
2776 | Örebro | Orebro | 59.2803 | 15.2200 | Sweden | SE | SWE | Örebro | admin | 98573.0 | 1752223019 |
2782 | Linköping | Linkoping | 58.4100 | 15.6299 | Sweden | SE | SWE | Östergötland | admin | 96732.0 | 1752963378 |
2796 | Helsingborg | Helsingborg | 56.0505 | 12.7000 | Sweden | SE | SWE | Skåne | minor | 91304.0 | 1752789933 |
2783 | Jönköping | Jonkoping | 57.7713 | 14.1650 | Sweden | SE | SWE | Jönköping | admin | 89780.0 | 1752079041 |
2802 | Norrköping | Norrkoping | 58.5954 | 16.1787 | Sweden | SE | SWE | Östergötland | minor | 88639.0 | 1752803583 |
Plot them on a map:
#Center on biggest Swedish city, Stocholm
number_of_largest_cities = 10
sorted_n_largest_cities = swedish_cities.sort_values(by="population", ascending=False)[:number_of_largest_cities]
lat = sorted_n_largest_cities.iloc[0].lat
long = sorted_n_largest_cities.iloc[0].lng
m = folium.Map(
location=[lat, long],
zoom_start=5
)
for idx, city in sorted_n_largest_cities.reset_index().iterrows():
color = mcolors.rgb2hex(plt.cm.Set1.colors[idx % len(plt.cm.Set1.colors)])
folium.CircleMarker(
location=[city.lat, city.lng],
radius=city.population / swedish_cities.population.max() * 30,
fill=True,
fill_color=color,
color=color,
popup="City: {}. Population: {:,}".format(city.city, int(city.population))
).add_to(m)
m
And now you have a nice plot of Swedish cities with circle sizes based on population.