Adithya
Adithya

Reputation: 1857

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

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')

Upvotes: 4

Views: 9977

Answers (1)

tacaswell
tacaswell

Reputation: 87566

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()

Upvotes: 9

Related Questions