scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.28k stars 502 forks source link

Python PPTX suppress zero values in a stacked Bar Chart #859

Open Codecrimper opened 1 year ago

Codecrimper commented 1 year ago

Is there a possibility to suppress zero values in a stacked bar chart like the following.

stacked_bar_chart

Manually, it's at least possible to remove the zero values in the Excel workbook while keeping the order (bad, medium, good). I tried to replace the zeros in the numpy array by NaN and then adding it by using add_series. As soon as I want to replace the data, I get the following error:

{TypeError} NAN/INF not supported in write_number() without 'nan_inf_to_errors' Workbook() option

My code snippet looks like:

chart_data = ChartData()
chart_data.categories = labels
chart = graphic_frame.chart

for index, row in df.iterrows():
    values = row.values
    values[values == 0] = np.nan
    chart_data.add_series(index, (values))

chart.replace_data(chart_data)

Or is there an alternative going deeper in the xml code to hide specifically zero labels?

Link to stackoverflow: https://stackoverflow.com/questions/73853536/python-pptx-suppress-zero-values-in-a-stacked-bar-chart

Dasc3er commented 10 months ago

The trick I found to work on my cases is to set the value to None in these situations. This could work to set the label to empty too depending on how the label is set up.

I am not sure if this will match perfectly your use case, but it is worth a test.