show multiple location in a map in python

To show multiple locations on a map in Python, we can use the folium library. The following code will take latitude and longitude coordinates for multiple locations, create markers for each location, and display them on a map.

main.py
import folium

# Create map object
map = folium.Map(location=[latitude, longitude], zoom_start=10)

# Define coordinates for each location
location_1 = [lat1, long1]
location_2 = [lat2, long2]
location_3 = [lat3, long3]
location_4 = [lat4, long4]

# Add markers to the map
folium.Marker(location=location_1, popup='Location 1').add_to(map)
folium.Marker(location=location_2, popup='Location 2').add_to(map)
folium.Marker(location=location_3, popup='Location 3').add_to(map)
folium.Marker(location=location_4, popup='Location 4').add_to(map)

# Display the map
map
564 chars
20 lines

We can also use the Matplotlib library to create a scatter plot of the locations on a map. In this case, we will need to convert the latitude and longitude coordinates to x and y coordinates on the plot.

main.py
import matplotlib.pyplot as plt

# Define coordinates for each location
location_1 = [lat1, long1]
location_2 = [lat2, long2]
location_3 = [lat3, long3]
location_4 = [lat4, long4]

x = [location_1[1], location_2[1], location_3[1], location_4[1]]
y = [location_1[0], location_2[0], location_3[0], location_4[0]]

# Create plot and add scatter plot points
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x, y)

# Set x and y limits
ax.set_xlim(min(x) - 0.1, max(x) + 0.1)
ax.set_ylim(min(y) - 0.1, max(y) + 0.1)

# Set axis labels and title
ax.set_xlabel('Longitude')
ax.set_ylabel('Latitude')
ax.set_title('Locations on a Map')

# Show plot
plt.show()
678 chars
28 lines

gistlibby LogSnag