Python ReportLab库:使用Piechart创建饼图
ReportLab是一个Python库,用于生成PDF文档。它可以用来创建各种类型的图表,包括饼图。下面是一个简单的示例代码,用于生成一个饼图:
from reportlab.graphics.charts.piecharts import Pie
from reportlab.graphics.shapes import Drawing
from reportlab.lib import colors
data = [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]
drawing = Drawing(width=400, height=200)
chart = Pie()
chart.x = 150
chart.y = 50
chart.width = 200
chart.height = 200
chart.data = [row[0] for row in data]
chart.labels = [row[1] for row in data]
chart.slices.strokeWidth = 0.5
chart.slices[0].popout = 10
chart.slices[0].strokeWidth = 2
chart.slices[0].strokeDashArray = [2, 2]
chart.slices[0].labelRadius = 1.75
chart.slices[0].fontColor = colors.red
chart.slices[1].fontColor = colors.yellow
chart.slices[2].fontColor = colors.blue
chart.slices[3].fontColor = colors.green
chart.slices[4].fontColor = colors.purple
drawing.add(chart)
这个代码将生成一个包含5个数据点的饼图,每个数据点都有一个标签。第一个数据点将突出显示,而其他数据点将以不同的颜色显示。这个代码可以通过调整变量来自定义饼图的大小、颜色、标签等。生成的饼图可以保存为PDF文件或直接在屏幕上显示。
原文地址: https://www.cveoy.top/t/topic/oNeJ 著作权归作者所有。请勿转载和采集!