简体   繁体   中英

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

在此输入图像描述

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

在此输入图像描述

How do I achieve this?

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

在此输入图像描述

See here for another example in the matplotlib documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM