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.min
和max
控制轴的整体比例,但这不是我想要的.
The bar chart I'm creating is auto scaling the Y axis tick increments starting at 0 as 0, .5, 1, 1.5
etc. I would like to have it increment in only whole numbers, ie 0, 1, 2, 3, 4
etc. I've tried chart.y_axis.tickLblSkip = 1
but had my doubts because I believe this is just the label itself, not the actual tick marks. Is there a way to control this? I see in the documentation you can control the overall scale of the axis with chart.y_axis.scaling.min
and max
but this isn't what I'm looking for.
以下是用于生成我的图表的代码.在该图的下方,您可以看到Y轴的滴答声,因为唯一作业"将只是一个整数,所以看起来很愚蠢,只有小数点:
Below is the code used to generate my chart. Below that is the chart itself where you can see the Y axis ticks, because 'unique jobs' will only be a whole number, it looks stupid to have decimals:
# 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
属性:
Your chart's y_axis
is of type NumericAxis
. To achieve what you want you will need to define its majorUnit
property:
chart.y_axis.majorUnit = 1
您如何知道某些类型的图表或轴可以使用哪些属性?
您需要弄清楚实现了什么.您的图表的类型为BarChart
.在BarChart上检查文档会得出其x轴是TextAxis
类型,而y轴是NumericAxis
.在此处中记录了所有轴类型的功能.显然,tickLblSkip
属性仅存在于TextAxis
和SeriesAxis
中.对于NumericAxis
,您将需要unit
属性,可能min
和max
也可能有用.
How do you know which properties are available for a certain type of chart or axis?
You'll need to figure out what's implemented. Your chart is of type BarChart
. Inspecting the 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. Apparently, the tickLblSkip
property is only present in TextAxis
and SeriesAxis
. For NumericAxis
you will need the unit
property, probably min
and max
might be useful, too.
type()
方法是确定任何对象类型的一种简单的交互式方法.放置
An easy, interactive way to determine any object's type is the type()
method. Putting
print(type(chart.y_axis))
在您的代码中将方便地显示:
in your code will conveniently reveal:
<class 'openpyxl.chart.axis.NumericAxis'>
这篇关于openpyxl-x和y轴刻度的增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!