[파이썬] bokeh 확대 및 축소 기능 활용하기

Bokeh is a powerful Python library for creating interactive visualizations. One of the key features of Bokeh is its ability to support zooming and panning interactions in your plots. In this blog post, we will explore how to utilize the zooming and panning functionality in Bokeh to provide enhanced user experiences in your visualizations.

Setting up Bokeh

Before we dive into zooming and panning, let’s make sure you have Bokeh installed. You can install Bokeh using pip:

pip install bokeh

Once you have Bokeh installed, you are ready to start creating interactive plots with zooming and panning capabilities.

Creating a Basic Plot

First, let’s create a basic Bokeh plot. We will use the figure function from the bokeh.plotting module to create a bare-bone plot:

from bokeh.plotting import figure, show

# Create a new plot with default tools enabled
p = figure()

# Add some data to the plot
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
p.line(x, y)

# Show the plot
show(p)

This code creates a simple line plot with five data points. When you run this code, you should see a static plot displayed in a new browser tab or window.

Adding Zooming and Panning

To enable zooming and panning in your plot, you can simply add the appropriate tools to your plot. Bokeh provides the WheelZoomTool and PanTool that allow users to zoom in/out and pan the plot, respectively.

To enable zooming, add the WheelZoomTool to the tools attribute of your plot:

from bokeh.models import WheelZoomTool

# Create a new plot with the WheelZoomTool enabled
p = figure(tools=[WheelZoomTool()])

# ...
# Add data and configure the plot as desired
# ...

# Show the plot
show(p)

With this modification, you should now be able to zoom in/out using your mouse scroll wheel when interacting with the plot.

To enable panning, add the PanTool to the tools attribute of your plot:

from bokeh.models import PanTool

# Create a new plot with the PanTool enabled
p = figure(tools=[PanTool()])

# ...
# Add data and configure the plot as desired
# ...

# Show the plot
show(p)

With this modification, you should now be able to pan the plot by clicking and dragging.

Further Customizations

Bokeh provides many other customization options for zooming and panning. You can control the zooming behavior (e.g., restrict zooming to only the x or y axis) and customize the appearance of the zoom/pan tools.

For example, to restrict zooming to the x-axis only, you can modify the x_wheel_zoomable property of the WheelZoomTool:

from bokeh.models import WheelZoomTool

# Create a new plot with restricted x-axis zooming
p = figure(tools=[WheelZoomTool(dimensions="width")])

# ...
# Add data and configure the plot as desired
# ...

# Show the plot
show(p)

Similarly, you can customize the appearance of the zoom/pan tools by modifying their properties, such as icon and active_icon.

For more information on additional customization options, you can refer to the Bokeh documentation.

Conclusion

In this blog post, we explored how to utilize the zooming and panning capabilities of Bokeh to create interactive visualizations. By adding the WheelZoomTool and PanTool to your plots, you can empower your users to explore and analyze the data in more detail. Bokeh makes it easy to enable these features and provides further customization options for fine-tuning your plots.

Give it a try and enhance your Python visualizations with Bokeh’s zooming and panning features!