Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openpyxl Hide Series Data Points from Legend

I have a chart I've created using openpyxl. Everything turns out as expected, however I would like to hide the data points from the legend while keeping their formats in the chart. In short, I would like to hide/remove all of the data points from the right hand side of the chart while preserving the colors of the bars.enter image description here

Here's a snippet of code used to build the chart:

chart1 = BarChart()
series = chart1.series[0]

ndcs = Set()
scfs = Set()

for i, row in enumerate(results):
    if 'DNDC' in row[0]: ndcs.add(i-1)
    if 'DSCF' in row[0]: scfs.add(i-1)


for index in ndcs:
    fill =  PatternFillProperties(prst="pct5")
    fill.background = ColorChoice(prstClr="red")

    pt = DataPoint(idx=index)
    pt.graphicalProperties.pattFill = fill
    series.dPt.append(pt)

for index in scfs:
    fill =  PatternFillProperties(prst="pct5")
    fill.background = ColorChoice(prstClr="blue")

    pt = DataPoint(idx=index)
    pt.graphicalProperties.pattFill = fill
    series.dPt.append(pt)

FWIW, I tried to follow the example from the docs here, however I couldn't find anything specific about hiding the information on the legend.

My question is, what members/attributes will I need to toggle off to hide the data points from showing on my legend while preserving the colors of the bars?

like image 663
E. Pratt Avatar asked Jan 29 '26 02:01

E. Pratt


2 Answers

I have to dive into the source code, the documentation is seriously lacking.

Try:

chart1.legend = None
like image 196
JuanPi Avatar answered Jan 31 '26 20:01

JuanPi


I think you need to set chart.legend.delete = True

like image 21
Charlie Clark Avatar answered Jan 31 '26 20:01

Charlie Clark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!