[파이썬] bokeh 그래프 내부의 애니메이션 효과 추가

Bokeh is a powerful Python library for creating interactive visualizations and data dashboards. It provides various tools and features to enhance the visual appeal and interactivity of your plots. One such feature is adding animation effects to your Bokeh graphs, which can captivate your audience and make your data come alive.

In this blog post, we will explore how to add animation effects to the elements within a Bokeh graph using Python.

Getting Started with Bokeh

Before we dive into adding animation effects, let’s first ensure that you have Bokeh installed on your system. You can install it using pip:

pip install bokeh

Once installed, we can begin creating our graph.

Creating a Basic Bokeh Graph

To create a basic Bokeh graph, we need to import the necessary modules and set up the plot layout. Here’s a code snippet to get started:

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

# Enable Bokeh for Jupyter Notebook
output_notebook()

# Create a new plot with title and axis labels
p = figure(title="My Bokeh Graph", x_axis_label="X", y_axis_label="Y")

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

# Show the plot
show(p)

Adding Animation Effects to Bokeh Graph

Now that we have our basic Bokeh graph, let’s add some animation effects to make it more dynamic and engaging. Bokeh provides different ways to add animations, such as changing colors, modifying plot attributes, and animating glyphs.

Changing Colors

To create a color-changing animation effect, we can use periodic callbacks in Bokeh. Here’s an example of how to achieve this:

from bokeh.plotting import curdoc
from bokeh.models import ColumnDataSource
from bokeh.models.callbacks import CustomJS

# Create a new figure and data source
p = figure(title="Color Animation", x_axis_label="X", y_axis_label="Y")
source = ColumnDataSource(data=dict(x=[], y=[]))

# Create a line glyph with a 'color' attribute linked to the data source
line = p.line(x='x', y='y', source=source, line_color="blue")

# Define the JavaScript code to change the line color periodically
callback = CustomJS(args=dict(line=line), code="""
    var colors = ["red", "green", "blue"];
    var i = 0;
    setInterval(function () {
        line.glyph.line_color = colors[i++ % colors.length];
    }, 1000);
""")

# Add the callback to the document
curdoc().add_periodic_callback(callback, 2000)

# Show the plot
show(p)

In this example, we create a line glyph with a ‘color’ attribute linked to a data source. Then, we define a JavaScript callback that changes the line color periodically using setInterval(). The callback is added to the document using curdoc().add_periodic_callback().

Modifying Plot Attributes

We can also animate other plot attributes, such as axis limits or titles. Let’s see an example:

from bokeh.models import Range1d

# Create a new figure
p = figure(title="Axis Animation", x_axis_label="X", y_axis_label="Y")

# Define starting and ending values for the x-axis range
start = 0
end = 10

# Create a Range1d object for the x-axis range
x_range = Range1d(start, end)

# Set the x-axis range of the plot
p.x_range = x_range

# Define the JavaScript code to animate the x-axis range
callback = CustomJS(args=dict(x_range=x_range), code="""
    var start = 0;
    var end = 10;
    var step = 0.1;
    var direction = 1;

    setInterval(function () {
        if (start >= end || start <= 0) {
            direction *= -1;
        }
        start += direction * step;
        end += direction * step;

        x_range.start = start;
        x_range.end = end;
    }, 100);
""")

# Add the callback to the document
curdoc().add_periodic_callback(callback, 200)

# Show the plot
show(p)

Here, we create a Range1d object for the x-axis range and animate it using a JavaScript callback. We use setInterval() to change the start and end values of the x-axis range periodically, thus creating an animation effect.

Animating Glyphs

Another way to add animation effects is by animating glyphs, such as circles or squares, on the plot. Here’s an example:

from bokeh.models import Circle

# Create a new figure
p = figure(title="Glyph Animation", x_axis_label="X", y_axis_label="Y")

# Create a circle glyph with size and color attributes
circle = Circle(x='x', y='y', size=10, fill_color="blue", line_color="black")

# Add the circle glyph to the plot
p.add_glyph(source.data, circle)

# Define the JavaScript code to animate the circle glyph
callback = CustomJS(args=dict(circle=circle), code="""
    var size = 10;
    setInterval(function () {
        if (size >= 20 || size <= 10) {
            size = 10;
        }
        size += 1;

        circle.size = size;
    }, 200);
""")

# Add the callback to the document
curdoc().add_periodic_callback(callback, 500)

# Show the plot
show(p)

In this example, we create a circle glyph with a ‘size’ attribute and animate it by changing the size value periodically. The glyph is added to the plot using p.add_glyph(), and the callback is added to the document using curdoc().add_periodic_callback().

Conclusion

Adding animation effects to your Bokeh graphs can greatly enhance the visual experience and make your data more engaging. With Bokeh’s powerful tools and features, you can easily create dynamic and interactive visualizations that captivate your audience.

In this blog post, we explored how to add animation effects to the elements within a Bokeh graph using Python. We covered changing colors, modifying plot attributes, and animating glyphs. Try experimenting with different animation techniques and unleash your creativity to create stunning and interactive Bokeh graphs.