使用Python中的PyChart库以(%)显示饼图的数据

使用Python中的PyChart库以(%)显示饼图的数据,python,charts,pie-chart,Python,Charts,Pie Chart,我正在使用Python中的库创建一个饼图。 这是我的密码: from pychart import * import sys data = [("foo", 10), ("bar", 20), ("baz", 30), ("ao", 40)] theme.use_color = True theme.get_options() ar = area.T(size = (150, 150), legend = legend.T(), x_grid_style = None

我正在使用Python中的库创建一个饼图。 这是我的密码:

from pychart import *
import sys

data = [("foo", 10), ("bar", 20), ("baz", 30), ("ao", 40)]
theme.use_color = True
theme.get_options()

ar = area.T(size = (150, 150), legend = legend.T(),
            x_grid_style = None, y_grid_style = None)

plot = pie_plot.T(data = data, arc_offsets = [0, 0, 0, 0], label_offset = 20, arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()

如何在此饼图上以(%)显示数据?

在将数据传递到绘图函数之前,是否将数据转换为百分比

例如:

def to_percents(data):
    total = float(sum(v for _, v in data))
    data[:] = [(k, v / total) for k, v in data]
    return data

data = to_percents([("foo", 1), ("bar", 3), ("baz", 5), ("ao", 7)])
print data
输出:

[('foo', 0.0625), ('bar', 0.1875), ('baz', 0.3125), ('ao', 0.4375)]

此外,它也不是很关键,但你应该做(k,v/total)*100是的,你可以做
(k,(v/total)*100.0)
,但Pychart的显示逻辑似乎并不关心数据的范围,只关心比率。