Creating a Slider for Folium Circle Map
In this article, we will guide you through the process of creating a slider for Folium Circle Map. We will explore how to add interactive sliders to your map and customize their behavior.
Introduction
Folium is an excellent library for creating interactive maps in Python. It allows us to create beautiful and informative maps by adding various overlays such as markers, polygons, heatmaps, and more. However, when working with large datasets or complex data visualization requirements, we may need additional interactivity. That’s where sliders come in – a perfect tool for allowing users to interactively explore our data.
In this article, we will create a Folium Circle Map and add two sliders: one to choose the day (Monday/Wednesday) and another to select the time (3pm/6pm/9pm). We’ll also cover how to implement the necessary code and integrate it into our map.
Prerequisites
Before we begin, ensure that you have the following libraries installed:
- Folium
- Pandas
- Matplotlib
- Plotly
- Scikit-plot
If you don’t have these libraries installed, you can install them using pip or conda.
# Install necessary libraries
pip install folium pandas matplotlib plotly scikit-plot
Creating the Dataframe and Map
Let’s create a sample dataframe df
that contains time series data for the number of cars passed from Monday to Wednesday for two different locations. We’ll also create a Folium map.
# Import necessary libraries
import pandas as pd
import folium
# Create sample dataframe
data = {
"day": ["Monday", "Tuesday", "Wednesday", "Monday", "Tuesday", "Wednesday"],
"lat": [3.1857, 3.1857, 3.1857, 3.1867, 3.1867, 3.1867],
"lng": [101.3153, 101.3153, 101.3153, 101.3145, 101.3145, 101.3145],
"3pm": [24, 13, 29, 7, 5, 19],
"6pm": [50, 36, 63, 15, 21, 24],
"9pm": [35, 49, 58, 32, 18, 42]
}
df = pd.DataFrame(data)
# Create Folium map
m = folium.Map(location=[3.185725088374699, 101.31522342062456], tiles="OpenStreetMap", zoom_start=15)
Adding Sliders
Now, let’s add two sliders to our map – one for the day (Monday/Wednesday) and another for the time (3pm/6pm/9pm).
# Import necessary libraries
import folium.plugins as plio
# Add slider for day
day_slider = plio.RangeSlider(
start=0,
end=2,
value=[1], # initial value is Monday (index 1)
min_value=0,
max_value=2,
color="blue",
)
# Add slider for time
time_slider = plio.RangeSlider(
start=0, # index of '3pm'
end=2, # index of '9pm'
value=[1], # initial value is '3pm' (index 1)
min_value=0,
max_value=2,
)
# Add sliders to the map
day_slider.add_to(m)
time_slider.add_to(m)
Finalizing the Code
Here’s the complete code:
import pandas as pd
import folium
from folium.plugins import RangeSlider
data = {
"day": ["Monday", "Tuesday", "Wednesday", "Monday", "Tuesday", "Wednesday"],
"lat": [3.1857, 3.1857, 3.1857, 3.1867, 3.1867, 3.1867],
"lng": [101.3153, 101.3153, 101.3153, 101.3145, 101.3145, 101.3145],
"3pm": [24, 13, 29, 7, 5, 19],
"6pm": [50, 36, 63, 15, 21, 24],
"9pm": [35, 49, 58, 32, 18, 42]
}
df = pd.DataFrame(data)
m = folium.Map(location=[3.185725088374699, 101.31522342062456], tiles="OpenStreetMap", zoom_start=15)
day_slider = RangeSlider(
start=0,
end=2,
value=[1],
min_value=0,
max_value=2,
color="blue",
)
time_slider = RangeSlider(start=0, # index of '3pm'
end=2, # index of '9pm'
value=[1], # initial value is '3pm' (index 1)
min_value=0,
max_value=2,
)
day_slider.add_to(m)
time_slider.add_to(m)
m
Example Use Cases
You can use this code to visualize and explore your data in various ways:
- Choose different days of the week by sliding the day slider.
- Select specific times (3pm, 6pm, or 9pm) by adjusting the time slider.
This example demonstrates how to create an interactive Folium Circle Map with two sliders using Python. You can customize and extend this code to suit your needs and explore different data visualization scenarios.
Last modified on 2024-10-02