openpyxl - x 和 y 轴刻度的增量

openpyxl - increment of x and y axis ticks

我正在创建的条形图自动缩放从 0 开始的 Y 轴刻度增量,如 0, .5, 1, 1.5 等。我希望它仅以整数递增,即 0, 1, 2, 3, 4 等. 我试过 chart.y_axis.tickLblSkip = 1 但我有疑问,因为我相信这只是标签本身,而不是实际的刻度线。有没有办法控制这个?我在文档中看到您可以使用 chart.y_axis.scaling.minmax 控制轴的整体比例,但这不是我要找的。

下面是用于生成我的图表的代码。下面是图表本身,您可以在其中看到 Y 轴刻度,因为 'unique jobs' 只会是一个整数,有小数看起来很愚蠢:

# Unique jobs chart
chart = BarChart()
chart.type = 'col'
chart.style = 13
chart.title = 'Unique Job Numbers Run'
chart.y_axis.title = 'Total Number of Parts'
chart.x_axis.title = 'Machine Number'
chart.legend = None
data = Reference(yearly_machine_totals_ws, min_col=4, min_row=1,
                 max_row=yearly_machine_totals_ws.max_row, max_col=4)
cats = Reference(yearly_machine_totals_ws, min_col=1, min_row=2,
                 max_row=yearly_machine_totals_ws.max_row)
chart.add_data(data, titles_from_data=True)
chart.set_categories(cats)
chart_ws.add_chart(chart, 'L2')

您图表的 y_axis 类型为 NumericAxis。要实现您想要的效果,您需要定义其 majorUnit 属性:

chart.y_axis.majorUnit = 1

您如何知道哪些属性可用于特定类型的图表或轴? 您需要弄清楚实施了什么。您的图表类型为 BarChart。检查 documentation on BarChart yields that its x axis is of type TextAxis, whereas the y axis is a NumericAxis. The features of all axis types are documented here。显然,tickLblSkip 属性 仅出现在 TextAxisSeriesAxis 中。对于 NumericAxis,您将需要 unit 属性,可能 minmax 也可能有用。

确定任何对象类型的一种简单、交互式的方法是 type() 方法。放

print(type(chart.y_axis))

在您的代码中将方便地揭示:

<class 'openpyxl.chart.axis.NumericAxis'>