How to remove a legend name in openpyxl

————— CODE SNIPPET—————

So the code goes like this. There’s some code to create a bar chart.

# create barChart

barChart= BarChart()
barChartData= Reference(worksheet, min_col=min_col, max_col=max_col, min_row=min_row, max_row=max_row)
barChart.add_data(bar_chart_data, from_rows=True, titles_from_data=True)

barChart.legend.position = 't'

… Then there’s some code to create a line chart.

# create lineChart
lineChart= LineChart()
lineChartData= Reference(worksheet, min_col=min_col + 1, max_col=max_col, min_row=max_row + 1,
                               max_row=max_row + 1)
lineChart.add_data(compare_chart_data, from_rows=True)

… Then there’s some code to add the line chart on top of the bar chart.

# combine both charts on top of each other
lineChart.y_axis.crosses = "min"
barChart+= lineChart

—————END OF SNIPPET—————

So what happens is, the chart results into having a legend that says

  • Bananas
  • Apples
  • Oranges
  • Series 4

Bananas, Apples and Oranges are the names of the data series of the bar chart. Series 4, is supposed to be the legend for the line chart. I wanna remove it, and I’ve tried to do below items.

I’m fairly new to python and I have no idea how to remove the legend “Series 4” in the table that’s being created by openpyxl.

barChart += lineChart

I’ve tried setting below code (after the addition), but it removes everything.

barChart.legend = None

I’ve also tried setting below code (before the addition), but it does nothing.

lineChart.legend = None

Somehow, I feel like it auto – creates the legend name Series 4 during the addition…

For the sake of testing, I tried to set

titles_from_data = True

But what this does is just change the legend name to 0%.

I don’t want to set it to another value, I just want to get rid of it.

—– UPDATE —–

I found a link in the comments section recommended by WBM to try and update the record inside the LegendEntry attribute. It looks something like this.

bar_chart.legend.LegendEntry = [(openpyxl.chart.legend.LegendEntry(3, delete=1))]

Still to no avail.

For investigation, I tried to print out the contents of the LegendEntry attribute prior to me adding the above code, and after.

Before adding the code, surprisingly, the result is empty.


After I added my code, it looked like something like this.

Parameters: [idx=3, delete=True, txPr=None]

Despite the LegendEntry being updated, the output still remains the same:

  • Bananas
  • Apples
  • Oranges
  • Series 4


Following your error code to the openpyxl documentation, we find the usage:


So you enter in the legendEntry the list you want to update the figure to. I believe in your case this will be

chartABC.legend.Legend(legendEntry=(['test', 'test', 'test']))

Leave a Reply

Your email address will not be published. Required fields are marked *