[파이썬] bokeh 원 그래프 작성하기

Bokeh is a powerful data visualization library in Python that allows you to create interactive and visually appealing plots. In this tutorial, we will see how to create a 원 그래프 (Pie chart) using Bokeh.

Step 1: Installation

Before we begin, make sure you have Bokeh installed on your system. You can install it using pip:

pip install bokeh

Step 2: Importing the necessary modules

To create a 원 그래프, we need to import the figure class and the output_file function from the Bokeh library:

from bokeh.plotting import figure, output_file, show

Step 3: Prepare the data

Next, we need to prepare the data for our 원 그래프. In this example, let’s assume we have a dataset that represents the expenses for a month:

import pandas as pd

data = pd.DataFrame({
    'category': ['Food', 'Rent', 'Utilities', 'Entertainment'],
    'amount': [500, 1000, 300, 200]
})

Step 4: Create the 원 그래프

Now, let’s create the 원 그래프 using Bokeh. First, we need to specify the output file:

output_file("pie_chart.html")

Then, we initialize a figure object and configure it for 원 그래프:

p = figure(title="Expense Categories", toolbar_location=None,
           tools="hover", tooltips="@category: @amount")

Next, we create the 원 그래프 using the wedge function:

p.wedge(x=0, y=0, radius=0.4,
        start_angle=cumsum('amount', include_zero=True),
        end_angle=cumsum('amount'),
        line_color="white", fill_color='color',
        legend_field='category', source=data)

Finally, we call the show function to display the 원 그래프:

show(p)

Step 5: Run the code

Save the code in a Python file, for example, pie_chart.py, and run it using a Python interpreter. This will generate an HTML file (pie_chart.html) containing the 원 그래프.

Conclusion

In this tutorial, we have learned how to create a 원 그래프 using Bokeh in Python. Bokeh provides a wide range of customization options, allowing you to create beautiful and interactive plots to visualize your data.