Matplotlib:如何在不改变条形大小的情况下增加条形的线宽?

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

生产

我想用条形的外边缘(相对于边缘的中心线)来表示数据值:

如何实现?

我认为使用 linewidth 属性 没有任何方法可以做到这一点,因为线条的笔划总是关于线条的中心对称。

一个稍微复杂的解决方法是使用代表柱的 matplotlib.patches.Rectangle 对象的 set_clip_path() 方法:

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 matplotlib 文档中的另一个示例。