Home > Software engineering >  How to set distance between bars in a horizontal bar diagram
How to set distance between bars in a horizontal bar diagram

Time:10-24

I want to plot a bar diagram from a python counter.

toplist = countersum.most_common(len(countersum))
topkeylist = list()
topvallist = list()
for i in range(len(toplist)):
    topkeylist.append(toplist[i][0])
    topvallist.append(toplist[i][1])
for i in range(len(toplist)):
    print(topkeylist[i],":" ,topvallist[i])

keylist = list()
for key in topkeylist:
    keylist.append(str(key))

plt.barh(keylist[:100], topvallist[:100], height = 0.3, align = 'center')
plt.xlabel('Occurence inside the dataset')
plt.ylabel('N-gram sample')
plt.show()

It gives me the following plot:

enter image description here

I would like to set the spaces between bars so they can even touch and also align the text to the bar - as in the text size would be the same as the width of the bar to read it better.

CodePudding user response:

you can adjust the width of the bars by using the width parameter here:

plt.barh(keylist[:100], topvallist[:100], height = 0.3, align = 'center', width=5).

Also, try reducing the font size on the y-axis so that the tick marks are clearer:

plt.yticks(fontsize=10).

If this does not work, you can increase the size of the figure using

fig = plt.figure(figsize=(18,18))

CodePudding user response:

You can set what you want in a dict and use enter image description here

  • Related