Change capstyle for errorbars in matplotlib

你。 提交于 2019-12-06 07:10:43
plotline, cap, barlinecols =\
         plt.errorbar([1,2,3], [2,3,4], yerr=[1,1,1], fmt=None, linewidth=3, capsize=0)

plt.errorbar returns 3 objects. plotline and cap are Line2D objects, which you can then do:

plotline.set_capstyle('round')
cap.set_capstyle('round')

barlinecols is a LineCollection object. However, the current version (matplotlib 2.0) does not support changing capstyle in LineCollection objects (see: https://github.com/matplotlib/matplotlib/issues/8277). But it looks like this will be implemented in the next version.

To give a working code here to change the capstyle of the vertical errorbar lines:

import matplotlib.pyplot as plt

plotline, caps, barlinecols =\
         plt.errorbar([1,2,3], [2,3,4], yerr=[1,1,1], linewidth=5, capsize=0)

plt.setp(barlinecols[0], capstyle="round", color="orange")

plt.show()

To instead change the capstyle of the errorbar caps, one would need to use some private attributes,

import matplotlib.pyplot as plt

plotline, caps, barlinecols =\
         plt.errorbar([1,2,3], [2,3,4], yerr=[1,1,1], linewidth=1, capsize=8, capthick=5)

for cap in caps:
    cap._marker._capstyle = "round"

plt.show()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!