[파이썬] bokeh 타일드 지도와 통합하기

Bokeh is a powerful Python library for creating interactive visualizations and data plots. One useful functionality it offers is the ability to integrate tile maps into your visualizations. In this blog post, we will explore how to integrate tiled maps into your Bokeh plots.

What are tile maps?

Tile maps, also known as raster maps or tiled web maps, are a popular way to visualize geographical data on the web. They consist of a grid of image tiles, where each tile represents a specific region on the map. These tiles are loaded and displayed dynamically to provide a smooth and interactive mapping experience for users.

Using Bokeh and tiled maps together

To integrate tiled maps into our Bokeh plots, we need to make use of the bokeh.tile_providers module, which provides access to various tile map providers. This module includes popular providers such as CartoDB Positron, CartoDB Dark Matter, and Stamen Toner.

Let’s see an example of how to create a Bokeh plot with a tiled map:

import bokeh.plotting as plt
from bokeh.tile_providers import CARTODBPOSITRON

tile_provider = CARTODBPOSITRON

# Create a Bokeh figure
p = plt.figure(x_range=(-200000, 6000000), y_range=(-1000000, 7000000),
               x_axis_type="mercator", y_axis_type="mercator")

# Add the tile map to the figure
p.add_tile(tile_provider)

# Additional plot configurations
p.title.text = "Bokeh Tiled Map Example"
p.xaxis.visible = False
p.yaxis.visible = False

# Show the plot
plt.show(p)

In this example, we import the necessary modules, specify the desired tile provider (CARTODBPOSITRON in this case), and create a Bokeh figure with the desired x and y ranges and axis types.

We then add the tile map to the figure using the add_tile() function and customize the plot further by setting a title and hiding the x and y axes. Finally, we show the plot using plt.show().

Customizing the map and adding data

You can further customize the appearance of the map by adjusting the range and axis types to suit your needs. Bokeh also allows you to overlay your own data onto the map, such as scatter plots, lines, or polygons, to create powerful visualizations.

# Assume 'source' is a Pandas DataFrame or similar data source
p.circle(x='longitude', y='latitude', source=source,
         fill_color='blue', size=5, alpha=0.8)

# More plot configurations
p.add_tools(plt.PanTool(), plt.WheelZoomTool(), plt.BoxZoomTool())

In the above code snippet, we assume we have a data source named source, which contains latitude and longitude information. We can use this data to plot circles on top of the tiled map by providing the x and y attributes, and customize the appearance with properties like fill_color, size, and alpha.

Additionally, we can add interactive tools to the plot, allowing users to pan and zoom the map, using the add_tools() function.

Conclusion

In this blog post, we explored how to integrate tiled maps into your Bokeh plots using the bokeh.tile_providers module. We learned how to create a basic tiled map plot, customize the appearance of the map, and overlay additional data.

With the power of Bokeh and tiled maps, you can create interactive and dynamic visualizations that allow users to explore geographical data in a meaningful way. Start experimenting with tiled maps in your Bokeh projects and unlock the potential of visualizing location-based data.