matplotlib bar chart: space out bars matplotlib bar chart: space out bars python-3.x python-3.x

matplotlib bar chart: space out bars


Try replace

plt.bar(range(len(my_dict)), my_dict.values(), align='center')

with

plt.figure(figsize=(20, 3))  # width:20, height:3plt.bar(range(len(my_dict)), my_dict.values(), align='edge', width=0.3)


There are 2 ways to increase the space between the barsFor reference here is the plot functions

plt.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Decrease the width of the bar

The plot function has a width parameter that controls the width of the bar. If you decrease the width the space between the bars will automatically reduce. Width for you is set to 0.8 by default.

width = 0.5

Scale the x-axis so the bars are placed further apart from each other

If you want to keep the width constant you will have to space out where the bars are placed on x-axis. You can use any scaling parameter. For example

x = (range(len(my_dict)))new_x = [2*i for i in x]# you might have to increase the size of the figureplt.figure(figsize=(20, 3))  # width:10, height:8plt.bar(new_x, my_dict.values(), align='center', width=0.8)


This answer changes the space between bars and it also rotate the labels on the x-axis. It also lets you change the figure size.

fig, ax = plt.subplots(figsize=(20,20))# The first parameter would be the x value, # by editing the delta between the x-values # you change the space between barsplt.bar([i*2 for i in range(100)], y_values)# The first parameter is the same as above, # but the second parameter are the actual # texts you wanna displayplt.xticks([i*2 for i in range(100)], labels)for tick in ax.get_xticklabels():    tick.set_rotation(90)