Hello everyone, today we will talk about how to perform map visualization and interactive geographical analysis using Python. If you are learning Python, you must be curious about data visualization. So, let’s explore the mysteries of Streamlit and Folium and embark on an interesting journey of Python map visualization!
Streamlit: A Powerful Tool for Python Web Application Development
Streamlit is a Python library for quickly creating interactive web applications. It allows us to easily build beautiful and practical web applications without needing to delve deep into front-end development.
Installing Streamlit is very simple; just enter the following command in the terminal:
pip install streamlit
Next, create a Python file, for example, <span>app.py</span>
, and then import Streamlit:
import streamlit as st
Now, we can start using various components of Streamlit, such as text, buttons, dropdown menus, etc., to build an interactive web application.
Tip: Streamlit automatically refreshes the application by default, so every time you save the code file, the application in the browser will automatically update. This greatly improves development efficiency.
Folium: The Magic Tool for Python Map Visualization
Folium is a library for creating interactive maps in Python. It is based on Leaflet.js, allowing us to easily add markers, popups, layers, and other elements to the map.
Installing Folium is also very simple:
pip install folium
Next, we can create a map object and set the initial position and zoom level:
import folium
m = folium.Map(location=[latitude, longitude], zoom_start=zoom_level)
Then, we can add various elements to the map, such as markers:
folium.Marker([latitude, longitude], popup='Marker Name').add_to(m)
Finally, save the map as an HTML file:
m.save('map.html')
And just like that, a simple interactive map has been created!
Tip: Folium supports various map tiles, such as OpenStreetMap, Stamen Terrain, etc. We can specify the desired base map using the <span>tiles</span>
parameter.
Streamlit-Folium: A Solution for Python Geographical Analysis
Now, let’s combine Streamlit and Folium to create an interactive geographical analysis platform.
First, display the Folium map in the Streamlit application:
import streamlit as st
import folium
# Create map object
m = folium.Map(location=[latitude, longitude], zoom_start=zoom_level)
# Display map in Streamlit
st.markdown('**My Map Application**')
map_data = st_folium(m, width=700, height=500)
Next, we can add some interactive components, such as a dropdown menu, to let users select their locations of interest:
# Add dropdown menu
selected_location = st.selectbox('Select Location', ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen'])
# Update map based on selected location
if selected_location == 'Beijing':
m = folium.Map(location=[39.9042, 116.4074], zoom_start=12)
elif selected_location == 'Shanghai':
m = folium.Map(location=[31.2304, 121.4737], zoom_start=12)
elif selected_location == 'Guangzhou':
m = folium.Map(location=[23.1291, 113.2644], zoom_start=12)
else:
m = folium.Map(location=[22.5431, 114.0579], zoom_start=12)
# Add markers on the map
folium.Marker(m.location, popup=selected_location).add_to(m)
# Update map display
map_data.folium_map = m
This way, whenever a user selects a new location, the map will automatically update and display the corresponding position.
Tip: In addition to dropdown menus, Streamlit also provides various interactive components like sliders, checkboxes, etc. We can choose the appropriate components based on actual needs.
Practical Case: COVID-19 Pandemic Map Visualization
Now, let’s create a COVID-19 pandemic map visualization application using Streamlit and Folium.
First, we need to obtain the pandemic data for various countries. Here, we use a public API:
import requests
url = 'https://disease.sh/v3/covid-19/countries'
response = requests.get(url)
data = response.json()
Next, we create a basic map:
import folium
m = folium.Map(location=[20, 0], zoom_start=2)
Then, iterate through each country’s data to add markers and popups on the map:
for country in data:
lat = country['countryInfo']['lat']
long = country['countryInfo']['long']
confirmed = country['cases']
recovered = country['recovered']
deaths = country['deaths']
html = f"""
<h4>{country['country']}</h4>
<p>Confirmed: {confirmed}</p>
<p>Recovered: {recovered}</p>
<p>Deaths: {deaths}</p>
"""
iframe = folium.IFrame(html=html, width=200, height=100)
popup = folium.Popup(iframe, max_width=2650)
folium.CircleMarker(location=[lat, long], radius=10, popup=popup, fill_color='red', color='red', fill_opacity=0.7).add_to(m)
Finally, display the map in Streamlit:
import streamlit as st
from streamlit_folium import st_folium
st.markdown('**COVID-19 Pandemic Map**')
st_data = st_folium(m, width=700, height=500)
Now, an interactive COVID-19 pandemic map has been created! We can click on the markers on the map to view the pandemic data for each country.
Tip: In addition to CircleMarker, Folium also supports various marker types, such as Marker, Icon, etc. We can choose the appropriate marker based on our needs.
Summary
Today, we explored how to use Streamlit and Folium to create interactive map visualization applications. With simple code, we can quickly develop beautiful and practical Python web applications for displaying and analyzing geographical data.
Key Points Recap:
-
Streamlit is a Python library for quickly creating interactive web applications.
-
Folium is a library for creating interactive maps in Python.
-
Combining Streamlit and Folium can create a powerful geographical analysis platform.
-
We can use public APIs to obtain geographical data and visualize it on maps.
That’s all for today’s Python learning journey! Remember to practice coding and gain more hands-on experience. I believe that through continuous exploration and learning, you will become an outstanding Python geographical analyst!
Wishing everyone happy learning, and may your Python skills continue to improve!