[파이썬] bokeh 그룹화 및 계층 구조 데이터 시각화

Bokeh is a powerful data visualization library in Python that allows you to create interactive and aesthetically pleasing visualizations. In this blog post, we will explore how to use Bokeh to group and visualize hierarchical data.

Importing the necessary libraries

First, we need to import the necessary libraries to work with Bokeh. You can do this by using the following code:

from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, HoverTool, TapTool, OpenURL
from bokeh.io import output_notebook

Loading and preparing the data

Next, we need to load the data and prepare it for visualization. For this example, let’s say we have a dataset that contains information about employees in a company, including their names, departments, and job titles. We want to visualize the hierarchical structure of the company by grouping the employees based on their departments and job titles.

# Load the data into a pandas DataFrame (assuming you have pandas installed)
import pandas as pd

data = pd.read_csv('employees.csv')

# Group the employees by department and job title
grouped_data = data.groupby(['department', 'job_title']).size().reset_index(name='count')

Creating the visualization

Now, let’s create the visualization using Bokeh. We will use a TreeMap plot to represent the hierarchical structure of the data. Each rectangle in the plot represents a group, with its size proportional to the number of employees in that group.

# Create the Bokeh figure
p = figure(title='Company Structure', plot_width=800, plot_height=600, tools='hover,tap')

# Convert the grouped data to a hierarchical structure
source = ColumnDataSource(grouped_data)

# Add the TreeMap glyph to the figure
p.rect(x='level_0', y='level_1', width=1, height=1, source=source, alpha=0.6)

# Add hover tooltips to display additional information
hover = HoverTool(tooltips=[('Department', '@department'), ('Job Title', '@job_title'), ('Count', '@count')])
p.add_tools(hover)

# Add tap behavior to open a URL with more details
tap = TapTool(callback=OpenURL(url='https://www.example.com/employee/{@department}/{@job_title}'))
p.add_tools(tap)

# Show the plot
output_notebook()
show(p)

Interacting with the visualization

Now that we have created the visualization, we can interact with it. Hovering over a rectangle will display information about the corresponding department and job title. Clicking on a rectangle will open a URL with more details about that group.

Conclusion

In this blog post, we have learned how to use Bokeh to group and visualize hierarchical data. By leveraging the power of Bokeh, you can create interactive and visually appealing visualizations that effectively communicate complex information.