为什么我的字典数据在使用 openpyxl 制作的柱状图上显示不正确?

huangapple go评论134阅读模式
英文:

Why is my dictionary data not displaying correctly on a bar chart using openpyxl?

问题

以下是您要翻译的内容:

"Dictionary Data not displaying correctly on Barchart (openpyxl)."

我有一个字典,键是产品名称,值是每种产品的购买数量。由于某种原因,这些值在使用 openpyxl 创建的柱状图上无法正确显示。在创建柱状图之前,我已经打印了字典,所有的值都是正确的,但它们与柱状图不匹配。顺便提一下,如果可能的话,如何将y轴上的单位更改为整数?

为什么我的字典数据在使用 openpyxl 制作的柱状图上显示不正确?

这是字典的显示内容:

{'123': 2, '456': 1, '789': 1, '111': 0, '222': 1, '333': 1, '444': 1, '555': 1, '666': 0, '777': 1, '854': 0}

以下是代码:

for key in product_quantity:
    active_sheet.append([barcode_list[key], product_quantity[key]])

my_chart = BarChart()

data = Reference(worksheet=active_sheet,
                 min_row=1,
                 max_row=len(barcode_list),
                 min_col=2,
                 max_col=3)

my_chart.legend = None

my_chart.add_data(data, titles_from_data=True)
active_sheet.add_chart(my_chart, "A1")

categories = Reference(active_sheet, min_col=1, min_row=1, max_row=len(barcode_list))
my_chart.set_categories(categories)  # 在X轴上添加产品名称

# 添加图表标题
my_chart.title = "按数量销售的产品"
my_chart.x_axis.title = '产品'
my_chart.y_axis.title = '已售数量'

希望这有所帮助!

英文:

Dictionary Data not displaying correctly on Barchart (openpyxl).

I have a dictionary with key being product name and value being quantity purchased of each product. For some reason this won't display properly on a bar chart using openpyxl. I have printed the dictionary before creating barchart and all the values are correct but they do not match the barchart. Side note if possible how do I change the units on the y access to be whole numbers.

为什么我的字典数据在使用 openpyxl 制作的柱状图上显示不正确?

This is what the dictionary displays:

{'123': 2, '456': 1, '789': 1, '111': 0, '222': 1, '333': 1, '444': 1, '555': 1, '666': 0, '777': 1, '854': 0}

Here is the code:

for key in product_quantity:
    active_sheet.append([barcode_list[key],product_quantity[key]])

my_chart = BarChart()

data = Reference(worksheet=active_sheet,
                 min_row=1,
                 max_row=len(barcode_list),
                 min_col=2,
                 max_col=3)

my_chart.legend = None

my_chart.add_data(data, titles_from_data=True)
active_sheet.add_chart(my_chart, "A1")

categories = Reference(active_sheet, min_col=1, min_row=1, max_row=len(barcode_list))
my_chart.set_categories(categories)  # Adds the product names along the X-Axis

# Add Chart titles
my_chart.title = "Products Sold by Quantity"
my_chart.x_axis.title = 'Products'
my_chart.y_axis.title = 'Quantity Sold'

答案1

得分: 0

你将 titles_from_data 设置为 True,因此 BarChartadd_data 方法期望数据的标题位于第一行;在你的示例中,它将第一行的值视为标题。(你可以通过打开生成的 Excel 文档并查看点击柱形图时引用的单元格来调试此问题。)

看起来你的数据是这样的:

barcode_list = {
    "123": "Milk (2L)",
    "456": "Bread (500g)",
    "789": "Juice (1L)",
    "111": "Bananas (2kg)",
    "222": "Apples (1kg)",
    "333": "Donuts (500g)",
    "444": "Kombucha (350ml)",
    "555": "Lemonade (1L)",
    "666": "Beef Mince (500g)",
    "777": "Chicken Breast (1kg)",
}

product_quantity = {
    "123": 2,
    "456": 1,
    "789": 1,
    "111": 0,
    "222": 1,
    "333": 1,
    "444": 1,
    "555": 1,
    "666": 0,
    "777": 1,
}

我更喜欢在写入数据时更加明确,所以如果 ws 是我的活动工作表,我可能会这样做:

# 标题
ws["A1"] = "Product"
ws["B1"] = "Quantity Sold"

# 数据
for i, (k, v) in enumerate(product_quantity.items()):
    row = i + 2
    ws[f"A{row}"] = barcode_list[k]
    ws[f"B{row}"] = v

在这种情况下,我确切地知道我的数据在哪里,可以看到你的 reference 正在查看正确的位置:

data = Reference(
    worksheet=ws,
    min_row=1,
    max_row=len(barcode_list) + 1,
    min_col=2,
    max_col=2,
)

我们可以使用以下方式将 BarChart 对象定向到此数据:

my_chart.add_data(data, titles_from_data=True)

还要更新你的 categories 引用:

categories = Reference(
    ws,
    min_col=1,
    min_row=2,
    max_row=len(barcode_list) + 1,
)

关于设置 y 轴上的单位,请使用:

my_chart.y_axis.minorUnit = 1

其他部分应该没有问题。

英文:

You set titles_from_data to True, so the add_data method of BarChart expects the heading of the data in the first row; in your example, it is treating the value in the first row as a heading. (You can debug this by opening up the generated Excel document and seeing which cells are referenced when you click on the bars.)

It looks like your data is something like:

barcode_list = {
    "123": "Milk (2L)",
    "456": "Bread (500g)",
    "789": "Juice (1L)",
    "111": "Bananas (2kg)",
    "222": "Apples (1kg)",
    "333": "Donuts (500g)",
    "444": "Kombucha (350ml)",
    "555": "Lemonade (1L)",
    "666": "Beef Mince (500g)",
    "777": "Chicken Breast (1kg)",
}

product_quantity = {
    "123": 2,
    "456": 1,
    "789": 1,
    "111": 0,
    "222": 1,
    "333": 1,
    "444": 1,
    "555": 1,
    "666": 0,
    "777": 1,
}

I prefer to be a little more explicit about where I would like to write the data, so, if ws is my active worksheet, I might do something like:

# Headings
ws["A1"] = "Product"
ws["B1"] = "Quantity Sold"

# Data
for i, (k, v) in enumerate(product_quantity.items()):
    row = i + 2
    ws[f"A{row}"] = barcode_list[k]
    ws[f"B{row}"] = v

In that case, I know exactly where my data is, and can see that your reference is looking at the right place:

data = Reference(
    worksheet=ws,
    min_row=1,
    max_row=len(barcode_list) + 1,
    min_col=2,
    max_col=2,
)

We can direct the BarChart object to this data using:

my_chart.add_data(data, titles_from_data=True)

Also update your categories reference:

categories = Reference(
    ws,
    min_col=1,
    min_row=2,
    max_row=len(barcode_list) + 1,
)

Regarding setting the units on the y axis, use:

my_chart.y_axis.minorUnit = 1

The rest should be okay.

huangapple
  • 本文由 发表于 2023年6月1日 13:56:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定