Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set matplotlib bar graph width when there is only one data

Tags:

matplotlib

I am using matplotlib to generate a graph. There is one problem while generating bar graph. If there is only one data like shown below,the width of the graph is covering the whole area,evenif the width is set. Example :

     x = [3] 
     y = [10] 
     bar(x, y, width=0.2,align='center')
like image 878
Adithya Avatar asked Nov 05 '25 17:11

Adithya


1 Answers

The width controls the width of the bar in 'data' units. By default the limits of the graph are auto-scaled to your data limits, in this case that is the width of your single bar. You just need the axis limits.

x = [3]
y = [10]
fig, ax = plt.subplots(1, 1)
ax.bar(x, y, width=0.2)
ax.set_xlim(0, 5)
plt.show()
like image 177
tacaswell Avatar answered Nov 07 '25 14:11

tacaswell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!