在 matplotlib 中绘制上限(下限)时如何改变箭头的大小?

问题描述 投票:0回答:1

我想用标记上限(或下限)的箭头表示不受约束的数据。为此,我生成一个布尔数组

lim
并将其输入为:
plt.errorbar(x, y, yerr=y_err, uplims=lim, ls='none')
。在默认情况下,箭头的长度不是我想要的。我希望它们在某些情况下更长,在另一些情况下更短。例如,在下图中,第一个数据点(棕色)中的箭头大小约为 0.5。由于另一个点(粉红色)位于箭头上方,因此不清楚是否存在箭头。我希望箭头大小改为 0.25。:

那么基本上,我如何手动控制箭头的大小?

python-3.x matplotlib plot errorbar
1个回答
0
投票

如果我们从这个错误栏示例的一部分开始,例如,

import matplotlib.pyplot as plt
import numpy as np

# example data
x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
y = np.exp(-x)
xerr = 0.1
yerr = 0.2

# lower & upper limits of the error
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
ls = 'dotted'

fig, ax = plt.subplots(figsize=(7, 4))

# including upper limits
eb = ax.errorbar(x, y, xerr=xerr, yerr=yerr, uplims=uplims,
                 linestyle=ls)

那么

eb
是一个

<ErrorbarContainer object of 3 artists>

如果我们看

eb.lines
我们会看到:

(<matplotlib.lines.Line2D at 0x7ff9e7520d30>,
 (<matplotlib.lines.Line2D at 0x7ff9e7522e90>,),
 (<matplotlib.collections.LineCollection at 0x7ff9e75211e0>,
  <matplotlib.collections.LineCollection at 0x7ff9e7522a40>))

该元组中的第二项是一个 1 元组,其中包含上限值的

Line2D
对象,例如,

eb.lines[1][0].get_xydata()
array([[ 1.        ,  0.16787944],
       [ 3.        , -0.15021293],
       [ 5.        , -0.19326205]])

因此,我们可以在这个

Line2d
对象中设置标记大小,以使箭头更大(从默认的 6 变为 10):

eb.lines[1][0].set_markersize(10)

给出:

© www.soinside.com 2019 - 2024. All rights reserved.