Display data in (%) for pie chart using the PyChart library in Python
I am creating a pie chart using PyChart library in Python. Here is my code:
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 = d开发者_运维知识库ata, arc_offsets = [0, 0, 0, 0], label_offset = 20, arrow_style = arrow.a3)
ar.add_plot(plot)
ar.draw()
How can I display data in (%) on this pie chart?
Would converting the data to percentages before passing it to the plot function work?
For example:
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
Output:
[('foo', 0.0625), ('bar', 0.1875), ('baz', 0.3125), ('ao', 0.4375)]
精彩评论