Customize Error Bars in Presentation Charts with Python

Overview

This article explains how to work with error bars in presentation charts by using Aspose.Slides. It shows how to add error bars to a chart series, configure X and Y error bar settings, and apply different value types such as fixed, percentage, and custom values.

It also demonstrates how to assign custom error bar values for individual data points in a series by using the corresponding data point collection. In addition, the article includes brief notes about how error bars behave during export, their compatibility with markers and data labels, and where to find the related API reference classes and enums.

Add Error Bar

Aspose.Slides for Python via .NET provides a simple API for managing error bar values. The sample code applies when using a custom value type. To specify a value, use the ErrorBarCustomValues property of a specific data point in the DataPoints collection of series:

  1. Create an instance of the Presentation class.
  2. Add a bubble chart on desired slide.
  3. Access the first chart series and set the error bar X format.
  4. Access the first chart series and set the error bar Y format.
  5. Setting bars values and format.
  6. Write the modified presentation to a PPTX file.
import aspose.slides.charts as charts
import aspose.slides as slides

# Creating empty presentation
with slides.Presentation() as presentation:
    # Creating a bubble chart
    chart = presentation.slides[0].shapes.add_chart(charts.ChartType.BUBBLE, 50, 50, 400, 300, True)

    # Adding Error bars and setting its format
    errBarX = chart.chart_data.series[0].error_bars_x_format
    errBarY = chart.chart_data.series[0].error_bars_y_format
    errBarX.is_visible = True
    errBarY.is_visible = True
    errBarX.value_type = charts.ErrorBarValueType.FIXED
    errBarX.value = 0.1
    errBarY.value_type = charts.ErrorBarValueType.PERCENTAGE
    errBarY.value = 5
    errBarX.type = charts.ErrorBarType.PLUS
    errBarY.format.line.width = 2
    errBarX.has_end_cap = True

    # Saving presentation
    presentation.save("ErrorBars_out.pptx", slides.export.SaveFormat.PPTX)

Add Custom Error Bar Value

Aspose.Slides for Python via .NET provides a simple API for managing custom error bar values. The sample code applies when the IErrorBarsFormat.ValueType property is equal to Custom. To specify a value, use the ErrorBarCustomValues property of a specific data point in the DataPoints collection of series:

  1. Create an instance of the Presentation class.
  2. Add a bubble chart on desired slide.
  3. Access the first chart series and set the error bar X format.
  4. Access the first chart series and set the error bar Y format.
  5. Access the chart series individual data points and setting the Error Bar values for individual series data point.
  6. Setting bars values and format.
  7. Write the modified presentation to a PPTX file.
import aspose.slides.charts as charts
import aspose.slides as slides

# Creating empty presentation
with slides.Presentation() as presentation:
    # Creating a bubble chart
    chart = presentation.slides[0].shapes.add_chart(charts.ChartType.BUBBLE, 50, 50, 400, 300, True)

    # Adding custom Error bars and setting its format
    series = chart.chart_data.series[0]
    errBarX = series.error_bars_x_format
    errBarY = series.error_bars_y_format
    errBarX.is_visible = True
    errBarY.is_visible = True
    errBarX.value_type = charts.ErrorBarValueType.CUSTOM
    errBarY.value_type = charts.ErrorBarValueType.CUSTOM

    # Accessing chart series data point and setting error bars values for individual point
    points = series.data_points
    points.data_source_type_for_error_bars_custom_values.data_source_type_for_x_plus_values = charts.DataSourceType.DOUBLE_LITERALS
    points.data_source_type_for_error_bars_custom_values.data_source_type_for_x_minus_values = charts.DataSourceType.DOUBLE_LITERALS
    points.data_source_type_for_error_bars_custom_values.data_source_type_for_y_plus_values = charts.DataSourceType.DOUBLE_LITERALS
    points.data_source_type_for_error_bars_custom_values.data_source_type_for_y_minus_values = charts.DataSourceType.DOUBLE_LITERALS

    # Setting error bars for chart series points
    for i in range(len(points)):
        points[i].error_bars_custom_values.x_minus.as_literal_double = i + 1
        points[i].error_bars_custom_values.x_plus.as_literal_double = i + 1
        points[i].error_bars_custom_values.y_minus.as_literal_double = i + 1
        points[i].error_bars_custom_values.y_plus.as_literal_double = i + 1

    # Saving presentation
    presentation.save("ErrorBarsCustomValues_out.pptx", slides.export.SaveFormat.PPTX)

FAQ

What happens to error bars when exporting a presentation to PDF or images?

They are rendered as part of the chart and preserved during conversion along with the rest of the chart formatting, assuming a compatible version or renderer.

Can error bars be combined with markers and data labels?

Yes. Error bars are a separate element and are compatible with markers and data labels; if elements overlap, you may need to adjust formatting.

Where can I find the list of properties and enums for working with error bars in the API?

In the API reference: the ErrorBarsFormat class and the related enums ErrorBarType and ErrorBarValueType.