serv-inc
serv-inc

Reputation: 38147

plt.bar bars are too far left, how to fix?

In help(plt.bar) of matplotlib version 2.0.0, it says that the bars are

[...] with rectangles bounded by:

  `left`, `left` + `width`, `bottom`, `bottom` + `height`
        (left, right, bottom and top edges)

Yet, calling plt.bar(0, 1) produces

enter image description here

which does not start at left == 0, but instead at left - width/2 == -0.4. How to fix this?

Upvotes: 0

Views: 236

Answers (1)

serv-inc
serv-inc

Reputation: 38147

Short answer:

plt.bar(0, 1, align="center")

Why:

The documentation on github says to use align=edge:

The default is now

(x - width/2, x + width/2, bottom, bottom + height)

[...] The align keyword-only argument controls if x is interpreted as the center or the left edge of the rectangle.[...]

align : {'center', 'edge'}, optional, default: 'center'
    If 'center', interpret the *x* argument as the coordinates
    of the centers of the bars.  If 'edge', aligns bars by
    their left edges
    To align the bars on the right edge pass a negative
    *width* and ``align='edge'``

Upvotes: 1

Related Questions