I want to create bars with this code and I've done it, but my percentages aren't visible correctly. They should be integer values, but they aren't
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import numpy as np
numbers1 = dataset.loc[0:36, 'Today'].tolist()
numbers2 = dataset.loc[0:36, 'Tomorrow'].tolist()
names = dataset.loc[0:36, 'Cities'].tolist()
data = list(zip(names, numbers1, numbers2))
data.sort(key=lambda x: x[1], reverse=True)
sorted_names, sorted_numbers1, sorted_numbers2 = zip(*data)
fig, ax = plt.subplots(figsize=(12, 6)) # Set the desired graphics dimensions (width, height)
width = 0.4
spacing = 0.1
x = np.arange(len(sorted_names))
rects1 = ax.bar(x - spacing/2, sorted_numbers1, width, color='blue')
for i in range(len(sorted_names)):
if sorted_numbers1[i] < sorted_numbers2[i]:
rects1[i].set_color('green')
elif sorted_numbers1[i] > sorted_numbers2[i]:
rects1[i].set_color('red')
ax.set_xticks(x)
ax.set_xticklabels(sorted_names, rotation=45, ha='right')
# Converting the "y" axis to percentages
ax.yaxis.set_major_formatter(mticker.PercentFormatter())
# Adding percentage values above bars
for i, rect in enumerate(rects1):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2, height, f'{sorted_numbers1[i]}%', ha='center', va='bottom')
# Setting the distances between elements
plt.subplots_adjust(bottom=0.4, wspace=0.5, hspace=0.5)
plt.show()
My bars with non correct percentages
My table with correct percentages
I expect to see normal percentages upon my bars like 74%....66% and next... and side percentages the same...