0

Currently I have the problem that I do not get the steps on the y-axis (score) changed. My representation currently looks like this:

enter image description here

However, since only whole numbers are possible in my evaluation, these 0.5 steps are rather meaningless in my representation. I would like to change these steps from 0.5 to 1.0. So that I get the steps [0, 1, 2, 3, ...] instead of [0.0, 0.5, 1.0, 1.5, 2.0, ...].

My code broken down to the most necessary and simplified looks like this:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# some calculations


x = np.arange(len(something))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, test1_means, width, label='Test 1')
rects2 = ax.bar(x + width/2, test2_means, width, label='Test 2')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Something to check')
ax.set_xticks(x, something)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()

plt.show()

In addition, after research, I tried setting the variable ax.set_yticks or adjusting the fig. Unfortunately, these attempts did not work.

What am I doing wrong or is this a default setting of matplotlib at this point?


Edit after comment:

My calculations are prepared on the basis of Excel data. Here is a reproducible code snippet with the current values how the code might look like in the final effect:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

stories = ["A", "B", "C", "D", "E"]
test1_means = [2, 3, 2, 3, 1]
test2_means = [0, 1, 0, 0, 0]

x = np.arange(len(stories))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, test1_means, width, label='Test 1')
rects2 = ax.bar(x + width/2, test2_means, width, label='Test 2')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Something')
ax.set_xticks(x, stories)
ax.legend()

ax.bar_label(rects1, padding=3)
ax.bar_label(rects2, padding=3)

fig.tight_layout()

plt.show()
41 72 6c
  • 1,600
  • 5
  • 19
  • 30

2 Answers2

1

Based on this answer, you just need to do:

from matplotlib.ticker import MaxNLocator

# set y-axis to only show integer values
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
Matt Pitkin
  • 3,989
  • 1
  • 18
  • 32
  • Thanks for your answer! That would work for my case, but isn't there another approach to this? What would be the case in another use case if I want to display all steps of 2 on the y-axis? So for example [0, 2, 4, 6, 8 ...] - then this solution would not work anymore. – 41 72 6c Jan 31 '23 at 10:56
1

You're looking for Axes.set_yticks. Add those lines right before the plot :

N = 1 # <- you can adjust the step here

ax.set_yticks(np.arange(0, max(test1_means + test2_means) + 1, N))

Output :

enter image description here

Timeless
  • 22,580
  • 4
  • 12
  • 30