'Why is bar plot in centre of picture/a axis with matplotlib?

I have a dictionary like this that i want to plot:

counts = {'set1': 110, 'set2': 12, 'set3': 661, 'set4': 34, 'set9': 2, 'set5': 7, 'set6': 23, 'set7': 53, 'set8': 65}

When I run:

counts = {'set1': 110, 'set2': 12, 'set3': 661, 'set4': 34, 'set9': 2, 'set5': 7, 'set6': 23, 'set7': 53, 'set8': 65}

df = pd.DataFrame.from_dict([counts])
ax = df.plot.bar()
ax.get_legend().remove()
plt.savefig('counts.png')

The output is: enter image description here

I'm trying to add labels to the x axis but when I do:

#ax.set_xticklabels(df.columns,rotation=45)

Only a '0' appears for the middle bar. I'm wondering if it's because the bars are bunched together in the centre of the image?

Could someone explain how to make the bars appropriately fit the plot space and not be centred in the middle (which might then help me with labelling)?



Solution 1:[1]

import matplotlib.pyplot as plt

counts = {'set1': 110, 'set2': 12, 'set3': 661, 'set4': 34, 'set9': 2, 'set5': 7, 'set6': 23, 'set7': 53, 'set8': 65}

plt.bar(range(len(counts)), list(counts.values()), align='center', color=['r', 'b', 'g', 'b', 'y', 'c', 'k', 'm'])
plt.xticks(range(len(counts))) 

plt.show()

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AlI