如何在openpyxl中删除图例名称

问题描述 投票:0回答:3

--------------- 代码片段--------------

所以代码是这样的。有一些代码可以创建条形图。

# 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'

... 然后是一些创建折线图的代码。

# 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)

... 然后有一些代码将折线图添加到条形图的顶部。

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

---------------片段结束---------------

所以发生的事情是,图表结果有一个图例,上面写着

  • 香蕉
  • 苹果
  • 橙子
  • 系列4

香蕉、苹果和橙子是条形图数据系列的名称。系列 4 应该是折线图的图例。我想删除它,并且我尝试过执行以下操作。

我对 python 相当陌生,我不知道如何删除 openpyxl 创建的表中的图例“Series 4”。

barChart += lineChart

我尝试设置下面的代码(添加后),但它删除了所有内容。

barChart.legend = None

我也尝试过设置下面的代码(在添加之前),但它没有任何作用。

lineChart.legend = None

不知何故,我觉得它在添加过程中自动创建了图例名称Series 4...

为了测试,我尝试设置

titles_from_data = True

但这只是将图例名称更改为 0%。

我不想将其设置为其他值,我只是想摆脱它。

-----更新-----

我在 WBM 推荐的评论部分找到了一个链接来尝试更新 LegendEntry 属性内的记录。看起来像这样。

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

还是没有效果。

为了进行调查,我尝试在添加上述代码之前和之后打印 LegendEntry 属性的内容。

在添加代码之前,令人惊讶的是,结果是空的。

[]

添加代码后,它看起来像这样。

参数:[idx=3,delete=True,txPr=None]

尽管 LegendEntry 已更新,但输出仍然保持不变:

  • 香蕉
  • 苹果
  • 橙子
  • 系列4
python openpyxl
3个回答
0
投票

我使用的是openpyxl 3.0.9。

查看源代码以获得一个想法并使其发挥作用:

import openpyxl
chart.x_axis = openpyxl.chart.axis.TextAxis(delete=True)

这里是更多代码:

CHART_ROW = 11
CHART_HEIGHT = 12
DATA_TABLE_START_ROW = 34
LENGTH_DIST_START_COL = 1
SPACER_DIST_START_COL = LENGTH_DIST_START_COL + 10
SPACER_RANGE_START_COL = SPACER_DIST_START_COL + 6

。 。 。 .

    chart = BarChart()
    chart.style = style  # 1 = black line, 13 = green bold line
    chart.title = "Spacer Distribution Summary"
    chart.height = CHART_HEIGHT  # default is 7.5
    chart.width = 7  # default is 15
    chart.y_axis.title = 'Target Spacer Frequency'
    chart.x_axis = openpyxl.chart.axis.TextAxis(delete=True)

    values = Reference(self.sheet,
                       min_col=SPACER_DIST_START_COL + 1,
                       max_col=SPACER_DIST_START_COL + 1,
                       min_row=self.spacer_distribution_first_row,
                       max_row=self.spacer_distribution_last_row)
    series = Series(values, title_from_data=False)
    chart.series.append(series)
    # chart.legend = None
    pos = f"{column_string(SPACER_DIST_START_COL)}{CHART_ROW}"
    self.sheet.add_chart(chart, pos)

0
投票

按照你的错误代码到openpyxl文档,我们找到了用法:

openpyxl.chart.legend.Legend(legendEntry=())

因此,您在图例中输入要将图形更新到的列表。我相信在你的情况下这会是

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

0
投票

我为这个确切的问题而苦苦挣扎,不得不梳理文档。

这对我有用

从 openpyxl.chart.series 导入 DataPoint、Series、DataLabelList、SeriesLabel

您的图表名称.dataLabels = DataLabelList()

您的图表名称.dataLabels.showSerName = False

© www.soinside.com 2019 - 2024. All rights reserved.