python pptx 在饼图中显示类别值

标签 python categories pie-chart python-pptx

如何切换下面标记为红色的参数(英文:python pptx 中的“showcategories”以显示饼图?任何想法表示赞赏。

MS Powerpoint parameter dialog box

最佳答案

显然有一个功能请求:https://github.com/scanny/python-pptx/issues/242

同时,可以通过从 python 设置标签文本来解决此问题,例如如下所示:

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION
from pptx.util import Inches

# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
    XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = False

# set labels to contain category and value
for i in range(len(chart_data.categories)):
    point = chart.series[0].points[i]
    point.data_label.text_frame.text = "{}: {:.0%}".format(chart_data.categories[i].label, chart.series[0].values[i])
    point.data_label.position = XL_LABEL_POSITION.OUTSIDE_END

prs.save('chart-01.pptx')

关于python pptx 在饼图中显示类别值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069581/

相关文章:

python - 如何使用 docker run 命令将 json 文件作为参数传递

类别选择中的mysql产品

sql - SQL - 查询以获取与每个类别和子类别关联的项目数

python - Plot.ly 饼图结果精度

Python 线程和 GIL

jquery - 用于与 jsonp 通信的简单 Python HTTP 服务器

attributes - Magento - 按类别获取可过滤的属性

android - 在 Android Studio 中创建彩色饼图

r - 完全删除 R 中的数据帧行。停止 table() 为删除的数据返回 0

python - 如何从 Python 中的二维散点图数据创建热图?