[파이썬] bokeh 그래프 내부에 다른 미디어 삽입하기

Bokeh is a powerful data visualization library in Python that allows you to create interactive and visually appealing graphs and plots. While Bokeh provides numerous options for customizing the appearance and behavior of graphs, it also supports the ability to insert other media, such as images or videos, within the graph.

Inserting Images in Bokeh Graphs

To insert an image into a Bokeh graph, you can make use of the image_url function provided by the bokeh.models module. This function allows you to specify the URL of the image and its position within the graph.

from bokeh.plotting import figure, show, output_file
from bokeh.models import ImageURL

# Create a new plot
plot = figure()

# Add an image to the plot
image = ImageURL(url="image.jpg", x=0, y=0, w=10, h=10)
plot.add_glyph(image)

# Save and show the plot
output_file("graph.html")
show(plot)

In the above example, we import the necessary modules and create a new Bokeh plot. We then create an instance of the ImageURL class and provide the URL of the image, as well as its position (x, y) and size (w, h). We then add the image glyph to the plot using the add_glyph method. Finally, we save the plot to an HTML file and show it.

Inserting Videos in Bokeh Graphs

Similarly, you can also insert videos into Bokeh graphs by leveraging the bokeh.embed module. This module provides the file_html function, which allows you to embed a video file directly into the graph.

from bokeh.plotting import figure, show
from bokeh.embed import file_html

# Create a new plot
plot = figure()

# Embed a video file in the plot
video_html = """
<video width="320" height="240" controls>
  <source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
"""
html = file_html(plot, resources=CDN, title="bokeh_video_example", template=video_html)
show(plot)

In the example above, we create a Bokeh plot and define the HTML code for embedding a video file. We then use the file_html function to generate the HTML code for the plot, using the specified template that embeds the video. Finally, we show the plot on the screen.

Conclusion

Bokeh provides you with the flexibility to include other media, such as images or videos, within your graphs. This feature allows you to enhance the visual representation of your data and create more interactive and engaging visualizations. Experiment with different combinations of media and graphs to create unique and captivating data visualizations using Bokeh.