Skip to main content
 首页 » 编程设计

python之在 matplotlib 中更改错误栏的 capstyle

2024年10月17日6powertoolsteam

我想将错误栏垂直线的 capstyle 设置为“圆形”。例如,以下代码生成一些带有错误条的点:

import matplotlib.pyplot as plt 
 
fig, ax = plt.subplots() 
plt.plot([1,2,3], [2,3,4], marker='o', linestyle='None') 
plt.errorbar([1,2,3], [2,3,4], yerr=[1,1,1], fmt=None, linewidth=3, capsize=0) 
plt.xlim([0,4]) 
plt.show() 

对于普通线条,我可以使用以下方法在 rcParams 中设置大写样式:

plt.rcParams['lines.dash_capstyle'] = 'round' 

我还找到了一些很好的例子,如何为 ticks 设置圆形 capstyles:

for i in ax.xaxis.get_ticklines(): i._marker._capstyle = 'round' 

但我无法为错误栏找到类似的方法。

请您参考如下方法:

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

plt.errorbar 返回 3 个对象。 plotline 和 cap 是 Line2D 对象,然后您可以执行以下操作:

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

barlinecols 是一个 LineCollection 对象。但是,当前版本 (matplotlib 2.0) 不支持更改 LineCollection 对象中的 capstyle(参见:https://github.com/matplotlib/matplotlib/issues/8277)。不过看起来这会在下一个版本中实现。