[파이썬] bokeh 색상 팔레트 활용

Bokeh is a powerful Python library for creating interactive visualizations for the web. It provides a wide range of tools and features to create stunning and engaging plots. One of the key aspects of creating visually appealing plots is selecting the right colors for your visualizations. In this blog post, we will explore how to leverage the color palettes provided by Bokeh to enhance your plots.

What is a color palette?

A color palette is a collection of colors that are visually harmonious and can be used together in a design or visualization. Color palettes can be monochromatic (shades of a single color), complementary (opposite colors on the color wheel), analogous (adjacent colors on the color wheel), or any combination thereof.

Bokeh provides a variety of built-in color palettes that you can use to customize the colors in your plots.

Using color palettes in Bokeh

To demonstrate how to use color palettes in Bokeh, let’s create a simple bar plot. We will use the Viridis color palette, which consists of a range of vibrant colors that are visually appealing and distinguishable.

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.palettes import Viridis

# Create a list of data
data = [10, 20, 30, 40, 50]

# Create a color palette
colors = Viridis[5]

# Create a figure
p = figure(x_range=['A', 'B', 'C', 'D', 'E'], plot_height=400, title="My Bar Plot")

# Add bars to the figure
p.vbar(x=['A', 'B', 'C', 'D', 'E'], top=data, width=0.9,
       color=colors)

# Save the plot to an HTML file
output_file("bar_plot.html")

# Show the plot
show(p)

In the above code, we import the necessary modules and functions from Bokeh. We define a list of data, data, which represents the heights of the bars in our bar plot. We then create the colors variable by accessing the Viridis color palette, which is a list of colors. This palette has 5 colors, corresponding to the 5 bars in our plot.

We create a figure object and pass the x_range parameter to define the x-axis categories. We also specify the plot_height and title of the plot. Next, we add the bars to the figure using the vbar function and pass the color parameter to set the colors of the bars.

Finally, we save the plot to an HTML file and display it using the show function.

Exploring more color palettes

Bokeh provides a rich collection of color palettes that you can use in your plots. Some of the popular ones include:

You can explore these color palettes and more in the Bokeh documentation.

Conclusion

Color palettes play a vital role in creating visually appealing and informative plots. Bokeh provides a wide range of color palettes that you can easily integrate into your Python code to enhance your visualizations. By leveraging these color palettes, you can create stunning and engaging plots that effectively communicate your data. So, go ahead and start exploring the world of color palettes in Bokeh to take your data visualizations to the next level.