python-pptx 实践 6.2:气泡图

0、import

import numpy as np
from pptx import Presentation
from pptx.util import Inches
from pptx.chart.data import BubbleChartDatafrom pptx.enum.chart import XL_CHART_TYPE

1、创建 ppt 添加图表

# 创建幻灯片-------------------------------------------------
prs = Presentation()    # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5]    # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout)    # 添加一页空白幻灯片
shapes = slide.shapes

shapes.title.text = '气泡图'

# 定义图表数据-------------------------------------------------
np.random.seed(42)
x = 10*np.random.rand(10)
np.random.seed(2)
y = 20*np.random.rand(10)
z = 0.5*x + 0.3*y

chart_data = BubbleChartData()

series = chart_data.add_series('Model1')
for e in zip(x, y, z):
    series.add_data_point(*e)
    
    
# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = slide.shapes.add_chart(chart_type=XL_CHART_TYPE.BUBBLE,  # 图表类型
                               x=left, y=top,    # 图表区的位置
                               cx=width, cy=height,    # 图表的宽和高
                               chart_data=chart_data)

# 保存 ppt 文档
prs.save('test.pptx')

ppt 效果:

 

 

posted @ 2020-02-22 21:49  赏尔  阅读(772)  评论(0编辑  收藏  举报