如何在Python中使用Bokeh库绘制饼图

如何在Python中使用Bokeh库绘制饼图

Bokeh是一个用于数据可视化的Python库。它使用HTML和JavaScript语言创建图表,并针对现代网页浏览器提供优雅、简洁的构造方式和高性能的交互功能。

在本教程中,我们将学习如何使用Bokeh库在Python中绘制饼图。虽然Bokeh库本身并没有提供直接绘制饼图的模块,但用户可以使用楔形图元素来创建饼图。

wedge()函数具有以下主要参数:

  • 楔形图的 xy 坐标
  • 楔形图的 半径
  • 楔形图的 起始角度
  • 楔形图的 结束角度

要绘制出看起来像饼图的楔形图,用户需要调整楔形图的 xy 坐标以及 半径 参数,并调整 起始角度结束角度 参数。

示例1

# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_file as OF
from bokeh.plotting import show

# Create a file for saving the model 
OF("JTP.html") 

# then, we will instantiate the figure object 
graph1 = fig(title = "Pie Chart using Bokeh") 

# initiate the center of the pie chart
x = 0
y = 0

# then, we will initiate the radius of the glyphs
radius = 1

# start angle values
start_angle = [0, 1.2, 2.1,
               3.3, 5.1]

# end angle values
end_angle = [1.2, 2.1, 3.3,
             5.1, 0]

# now, generate the color of the wedges
color1 = ["brown", "grey", "green",
          "orange", "red"]

# now, we will plot the graph
graph1.wedge(x, y, radius,
            start_angle,
            end_angle,
            color = color1)

# At last, we will display the graph
show(graph1)

输出:

如何在Python中使用Bokeh库绘制饼图

示例2

在此示例中,用户将可视化一些数据。数据包含了一家公司在2014-15年投资的领域的详细信息。领域如下:

  • 顾客: 5%
  • 创新: 1%
  • 股份: 2%
  • 市场推广: 1%
  • 技术: 1%

用户可以将 百分比 转换为 弧度 ,通过使用以下公式找到 起始角度结束角度 的值:

math.radians((percent / 100) * 360)

代码:

# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_file as OF
from bokeh.plotting import show
import math

# Create a file for saving the model 
OF("JTP.html") 

# then, we will instantiate the figure object 
graph1 = fig(title = "Pie Chart using Bokeh") 

# name of the fields for investment
fields = ["Customers", "Innovation", "Shares", "Marketing", "Technology"]

# % tage weightage of the sectors
percentages = [12.5, 32.1, 21.2, 15.1, 19.1]

# formula for converting percentage into radians
radians1 = [math.radians((percent / 100) * 360) for percent in percentages]

# Generating the start angle values
start_angle = [math.radians(0)]
prev = start_angle[0]
for k in radians1[:-1]:
    start_angle.append(k + prev)
    prev = k + prev

# generating the end angle values
end_angle = start_angle[1:] + [math.radians(0)]

# initiate the center of the pie chart
x = 0
y = 0

# then, we will initiate the radius of the glyphs
radius = 1

# now, generate the color of the wedges
color1 = ["pink", "yellow", "lightgreen",
          "orange", "red"]

# now, we will plot the graph
for k in range(len(fields)):
    graph1.wedge(x, y, radius,
                start_angle = start_angle[k],
                end_angle = end_angle[k],
                color = color1[k],
                legend_label = fields[k])

# At last, we will display the graph
show(graph1)

输出:

如何在Python中使用Bokeh库绘制饼图

结论

在本教程中,我们讨论了如何使用Python中的bokeh库绘制数据可视化的饼图。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程