[Python] Adjusting scale of bubbles in BubbleChart with python-pptx

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for help scaling the size of bubbles in a python-pptx bubble chart, so I have managed to plot a bubble chart using python-pptx and using the following data:

    Risk​
    Yield​
    R-adj​
    Type​
    0.10​
    0.15​
    0.015​
    Cash​
    0.40​
    0.50​
    0.025​
    Money Market​
    0.60​
    0.20​
    0.015​
    Government​
    2.20​
    5.30​
    0.065​
    HY Bonds​
    1.20​
    2.20​
    0.050​
    IG Bonds​
    1.45​
    3.50​
    0.053​
    Yield-Enhanced​
    2.20​
    3.50​
    0.044​
    Equity Infra​

    where 'data' variable below is a pandas.DataFrame object representing the table above

    from pptx import Presentation
    from pptx.chart.data import CategoryChartData, ChartData, BubbleChartData
    from pptx.enum.chart import (XL_CHART_TYPE, XL_LEGEND_POSITION, XL_LABEL_POSITION)
    from pptx.util import Inches, Pt

    prs = Presentation(r'template.pptx')
    slide = prs.slides[0]

    # Create bubble chart
    chart_data = BubbleChartData()
    for i, asset in enumerate(asset_type):
    series = chart_data.add_series(asset)
    x, y, z = x_series, y_series, z_series
    series.add_data_point(x,y,z*0.5) # changing z value doesn't change the size of the bubbles

    # x, y, cx, cy = Inches(10.8), Inches(4.0), Inches(0.4), Inches(2.4)
    x, y, cx, cy = Inches(0.5), Inches(2), Inches(10.8), Inches(4.3)
    chart = slide.shapes.add_chart(XL_CHART_TYPE.BUBBLE, x, y, cx, cy, chart_data).chart

    # Config legend
    chart.has_legend = True
    chart.legend.position = XL_LEGEND_POSITION.BOTTOM
    chart.legend.include_in_layout = False



    what I have done is trying to adjust the size of 'z', the size variable, however it seems that changing this value doesn't change the size of the bubbles as it seems that the size of bubbles are proportionate to the plot area, where the largest bubble is x% of the area of the plot, does anybody know how I could adjust the scale of the bubble?

    And also include data label on the bubbles, rather than having a legend?

    Thanks, any help would be appreciated.

    Continue reading...

Compartilhe esta Página