openpyxl - increment of x and y axis ticks

跟風遠走 提交于 2020-01-05 08:25:14

问题


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.

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


回答1:


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

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.

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


来源:https://stackoverflow.com/questions/59304386/openpyxl-increment-of-x-and-y-axis-ticks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!