wordpress

How to Plot a Pie Chart using Bokeh Library in Python – Step-by-Step Guide

1. Import the necessary libraries

In order to plot a pie chart using the Bokeh library in Python, we first need to import the necessary libraries. Bokeh is a powerful data visualization library that provides interactive and beautiful visualizations. We also need to import the necessary modules from the Bokeh library to create and customize the pie chart.

Here is the code to import the necessary libraries:

«`python
import numpy as np
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
«`

The numpy library is used to generate random data for the pie chart. The figure module from the bokeh.plotting library is used to create the figure object, which is the canvas on which we will plot the pie chart. The show module is used to display the chart. Finally, the output_notebook module is used to display the chart in the Jupyter notebook.

2. Prepare the data

Next, we need to prepare the data that we want to plot in the pie chart. In this example, we will generate random data using the numpy library. We will create an array of random numbers that sum up to 100, which represents the percentage of each category in the pie chart.

Here is the code to prepare the data:

«`python
data = np.random.randint(1, 10, size=5)
data = data / np.sum(data) * 100
«`

Recomendado:  Python Modulus Operator: Sintaxis y uso en Python

In this code, we generate an array of 5 random integers between 1 and 10 using the numpy.random.randint function. We then divide each element of the array by the sum of the array and multiply by 100 to get the percentage values.

3. Create a figure object

Once we have the data ready, we can create a figure object using the figure module from the bokeh.plotting library. The figure object is the canvas on which we will plot the pie chart.

Here is the code to create a figure object:

«`python
p = figure(plot_height=400, plot_width=400)
«`

In this code, we create a figure object with a height and width of 400 pixels. You can adjust the height and width according to your preference.

4. Plot the pie chart

Now that we have the figure object ready, we can plot the pie chart using the wedge glyph method of the figure object. The wedge method takes the following parameters:

  • x: The x-coordinate of the center of the pie chart.
  • y: The y-coordinate of the center of the pie chart.
  • radius: The radius of the pie chart.
  • start_angle: The starting angle of the first wedge in radians.
  • end_angle: The ending angle of the last wedge in radians.
  • color: The color of the wedges.

Here is the code to plot the pie chart:

«`python
p.wedge(x=0, y=0, radius=0.8, start_angle=0, end_angle=data, color=[‘red’, ‘green’, ‘blue’, ‘orange’, ‘purple’])
«`

In this code, we plot the pie chart at the center of the figure object with a radius of 0.8. The start angle is set to 0 and the end angle is set to the data array, which represents the percentage values of each category. We also specify the colors of the wedges using a list of colors.

Recomendado:  Context Manager in Python: A Guide on What It Is and How to Use It

5. Customize the chart

We can customize the pie chart by adding various visual elements such as a title, axis labels, and grid lines. We can also customize the appearance of the wedges by changing the line color, line width, and fill color.

Here is the code to customize the chart:

«`python
p.title.text = «Pie Chart»
p.title.text_font_size = «20px»
p.axis.visible = False
p.grid.visible = False
p.outline_line_color = None
p.wedge(line_color=’white’, line_width=2, fill_alpha=0.8)
«`

In this code, we set the title of the chart to «Pie Chart» and increase the font size to 20 pixels. We hide the axis and grid lines by setting their visibility to False. We also remove the outline line of the chart by setting the outline_line_color to None. Finally, we customize the appearance of the wedges by changing the line color to white, line width to 2, and fill alpha to 0.8.

6. Add labels and legends

We can add labels to the wedges of the pie chart to display the category names and their corresponding percentage values. We can also add a legend to the chart to provide a visual representation of the categories.

Here is the code to add labels and legends:

«`python
labels = [‘Category 1’, ‘Category 2’, ‘Category 3’, ‘Category 4’, ‘Category 5′]
p.text(x=0, y=0, text=labels, text_font_size=’12px’, text_baseline=’middle’, text_align=’center’)
p.legend.label_text_font_size = ’12px’
p.legend.location = ‘top_right’
«`

In this code, we create a list of labels for each category. We then add the labels to the center of the pie chart using the text method of the figure object. We specify the font size, baseline, and alignment of the labels. We also customize the font size of the legend labels and set the location of the legend to the top right corner of the chart.

Recomendado:  Python Program to Print Prime Factor - Código para factores primos

7. Show the chart

Finally, we can display the pie chart using the show module from the bokeh.io library. This will open a new browser tab or window and show the chart.

Here is the code to show the chart:

«`python
output_notebook()
show(p)
«`

In this code, we use the output_notebook module to display the chart in the Jupyter notebook. We then use the show module to display the chart.

That’s it! You have successfully plotted a pie chart using the Bokeh library in Python. You can now customize the chart further by adding more visual elements or exploring other features of the Bokeh library.

Autor

osceda@hotmail.com

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *