Felix Hoffmann
Felix Hoffmann

Reputation: 332

Matplotlib: How to increase linewidth of bar without changing its size?

import pylab as pl

pl.bar([1],[1], lw = 20., facecolor = 'white', edgecolor= 'red')
pl.plot([0,2],[0,0], 'k')
pl.plot([0,2],[1,1], 'k')

pl.xlim(0.8,2)
pl.ylim(-0.2,1.2)

pl.savefig('blw.png')

produces

enter image description here

I would like the outer edge of the bar (as opposed the centerline of the edge) to represent the data values:

enter image description here

How do I achieve this?

Upvotes: 3

Views: 480

Answers (1)

ali_m
ali_m

Reputation: 74154

I don't think there's any way to do this using the linewidth property, since a line's stroke is always symmetric about the center of the line.

A slightly hacky work-around would be to use the set_clip_path() method of the matplotlib.patches.Rectangle object representing the bar:

from matplotlib import pyplot as plt

fig, ax = plt.subplots(1, 1)

ax.hold(True)

patches = ax.bar([1],[1], lw = 20., facecolor = 'w', edgecolor= 'red')
ax.plot([0,2],[0,0], 'k')
ax.plot([0,2],[1,1], 'k')

ax.set_xlim(0.8,2)
ax.set_ylim(-0.2,1.2)

# get the patch object representing the bar
bar = patches[0]

# set the clipping path of the bar patch to be the same as its path. this trims
# off the parts of the bar edge that fall outside of its outer bounding
# rectangle
bar.set_clip_path(bar.get_path(), bar.get_transform())

enter image description here

See here for another example in the matplotlib documentation.

Upvotes: 2

Related Questions