[파이썬] bokeh 지리적 데이터 시각화

Bokeh is a versatile Python library for creating interactive visualizations. It provides a wide range of tools and features for visualizing geographical data, making it a popular choice for data scientists, researchers, and developers. In this blog post, we will explore how to use Bokeh for geographical data visualization in Python.

Prerequisites

Before we start, make sure you have Bokeh installed on your system. You can install it by running the following command:

pip install bokeh

You will also need some geographical data for visualization purposes. In this example, we will be using a dataset containing information about cities and their populations.

Creating a Basic Map

To create a map using Bokeh, we need to start by importing the necessary modules and loading the geographical data.

from bokeh.plotting import figure, show
from bokeh.tile_providers import get_provider
from bokeh.io import output_notebook

output_notebook()  # For Jupyter Notebook

# Define the map plot
plot = figure(x_range=(-2000000, 6000000), y_range=(-1000000, 7000000),
              x_axis_type="mercator", y_axis_type="mercator",
              plot_width=800, plot_height=500)

# Add the map tile
tile_provider = get_provider('CARTODBPOSITRON_RETINA')
plot.add_tile(tile_provider)

show(plot)

The code above sets up a basic map plot using the Mercator projection. It also adds an underlying map tile using the ‘CARTODBPOSITRON_RETINA’ tile provider. You can experiment with different tile providers to change the visual style of the map.

Plotting Geographical Points

To plot geographical points on the map, we need latitude and longitude coordinates. Let’s plot some random cities from our dataset.

# Import the data
cities = [
    {"name": "New York", "latitude": 40.7128, "longitude": -74.0060},
    {"name": "San Francisco", "latitude": 37.7749, "longitude": -122.4194},
    {"name": "London", "latitude": 51.5074, "longitude": -0.1278},
    {"name": "Tokyo", "latitude": 35.6895, "longitude": 139.6917}
]

# Add markers for cities
plot.circle(x=[city['longitude'] for city in cities],
            y=[city['latitude'] for city in cities],
            size=10, fill_color="blue", fill_alpha=0.8)

show(plot)

In the code above, we define a list of dictionaries containing city information. We then use the circle glyph to plot markers on the map for each city. Adjust the size, fill_color, and fill_alpha parameters to customize the appearance of the markers.

Adding Interactivity

One of the key features of Bokeh is its ability to create interactive visualizations. Let’s add some interactivity to our geographical plot by displaying the city names when hovering over the markers.

from bokeh.models import HoverTool

# Create a HoverTool instance
hover = HoverTool(tooltips=[("Name", "@name")])

# Add the HoverTool to the plot
plot.add_tools(hover)

show(plot)

With the code above, a tooltip will be displayed when hovering over a city marker, showing the city name.

Conclusion

In this blog post, we have seen how to create basic geographical visualizations using Bokeh in Python. We started by setting up a map plot and then plotted geographical points. We also added interactivity by displaying tooltips on hover.

Bokeh offers many more possibilities for geographical data visualization, such as plotting routes, heatmaps, and choropleth maps. Explore the official Bokeh documentation for more advanced techniques and examples.

Happy coding!