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')
产生
我希望条形的外边缘(与边缘的中心线相对)来表示数据值:
我如何实现这一目标?
答案 0 :(得分:2)
我认为没有办法使用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文档中的另一个示例。