3

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

enter image description here

Chris Macaluso
  • 1,372
  • 2
  • 14
  • 33

1 Answers1

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'>
Friedemann Hahn
  • 290
  • 2
  • 10
  • That was close, what I actually needed was `chart.y_axis.majorUnit = 1`. You did however point me in the right direction, I did not know about `NumericAxis`, or that I could check it with `type`. This helped to find what I need in the documentation. Adjust your answer and I will accept it. Thank you! – Chris Macaluso Dec 16 '19 at 11:29