Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get bar chart to plot in matplotlib

Using this code,

x = [420.0, 4353.0, 4373.0]; y = [269.0, 252.0, 283.0]
plt.bar(x,y)
plt.show()

I get:

Empty bar graph

Where are the bars? How do I get them to show up?

like image 946
lars Avatar asked Feb 12 '17 23:02

lars


People also ask

How do I plot a bar in Matplotlib?

The function makes a bar plot with the bound rectangle of size (x −width = 2; x + width=2; bottom; bottom + height). sequence of scalars representing the x coordinates of the bars. align controls if x is the bar center (default) or left edge. scalar or sequence of scalars representing the height(s) of the bars.

Why is my Matplotlib plot empty?

Because, when plt. show is executed, it displays figure on the screen and closes the figure. This is why you get an empty plot when you save it after plt. show.


1 Answers

By default, each bar is plotted with a width of 0.8 and due to the large range of x values on your plot, that is too narrow to be visible when rendered. You'll instead want to specify a larger width using the width kwarg

plt.bar(x, y, width=20)

enter image description here You an also use an array for the width value to specify a different width for each bar

plt.bar(x, y, width=[3000, 20, 20])
like image 163
Suever Avatar answered Sep 30 '22 04:09

Suever