Plotly:如何去除用 add_annotation() 创建的箭头尾部后面的间隙?
Plotly: How to remove the gap behind the tail of an arrow created with add_annotation()?
我正在使用 plotly.graph_objects.Figure.add_annotation()
函数将箭头添加到我的图中 - 请参阅下面的简单示例代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_annotation(ax = 3, axref = 'x', ay = 2, ayref = 'y',
x = 4, xref = 'x', y = 2, yref = 'y',
arrowhead = 3)
fig.add_shape(x0 = 1, x1 = 3, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.add_shape(x0 = 4, x1 = 6, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.update_layout(xaxis_range = [0, 7], yaxis_range = [0, 4])
fig.show()
呈现以下图像:
如您所见,左侧矩形和箭头尾部之间有一个小间隙。 有没有办法消除这个间隙,使箭头完全按照参数 ax
和 ay
的指定开始? 我试过查看 documentation 看看是否还有add_annotation()
函数的其他相关参数,但没找到线索。
应该这样做:
xanchor = 'right'
完整代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_annotation(ax = 3, axref = 'x', ay = 2, ayref = 'y',
x = 4, xref = 'x', y = 2, yref = 'y',
arrowhead = 3,
xanchor = 'right')
fig.add_shape(x0 = 1, x1 = 3, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.add_shape(x0 = 4, x1 = 6, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.update_layout(xaxis_range = [0, 7], yaxis_range = [0, 4])
fig.show()
解释:
根据 documentation、
xanchor – Sets the text box’s horizontal position anchor This anchor binds the x
position to the “left”, “center” or “right” of the annotation. For example, if x
is set to 1, xref
to “paper” and xanchor
to “right” then the right-most portion of the annotation lines up with the right-most edge of the plotting area. If “auto”, the anchor is equivalent to “center” for data- referenced annotations or if there is an arrow, whereas for paper-referenced with no arrow, the anchor picked corresponds to the closest side.
即使没有实际的注释文本,(place-holder) 文本字段仍会呈现(默认情况下锚定到 'center'
),这就是推动箭头尾部的原因。将 xanchor 设置为 'right'
可确保箭头从预期位置开始,同时将空文本字段推到其左侧。
我正在使用 plotly.graph_objects.Figure.add_annotation()
函数将箭头添加到我的图中 - 请参阅下面的简单示例代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_annotation(ax = 3, axref = 'x', ay = 2, ayref = 'y',
x = 4, xref = 'x', y = 2, yref = 'y',
arrowhead = 3)
fig.add_shape(x0 = 1, x1 = 3, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.add_shape(x0 = 4, x1 = 6, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.update_layout(xaxis_range = [0, 7], yaxis_range = [0, 4])
fig.show()
呈现以下图像:
如您所见,左侧矩形和箭头尾部之间有一个小间隙。 有没有办法消除这个间隙,使箭头完全按照参数 ax
和 ay
的指定开始? 我试过查看 documentation 看看是否还有add_annotation()
函数的其他相关参数,但没找到线索。
应该这样做:
xanchor = 'right'
完整代码:
import plotly.graph_objects as go
fig = go.Figure()
fig.add_annotation(ax = 3, axref = 'x', ay = 2, ayref = 'y',
x = 4, xref = 'x', y = 2, yref = 'y',
arrowhead = 3,
xanchor = 'right')
fig.add_shape(x0 = 1, x1 = 3, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.add_shape(x0 = 4, x1 = 6, xref = 'x',
y0 = 1, y1 = 3, yref = 'y')
fig.update_layout(xaxis_range = [0, 7], yaxis_range = [0, 4])
fig.show()
解释:
根据 documentation、
xanchor – Sets the text box’s horizontal position anchor This anchor binds the
x
position to the “left”, “center” or “right” of the annotation. For example, ifx
is set to 1,xref
to “paper” andxanchor
to “right” then the right-most portion of the annotation lines up with the right-most edge of the plotting area. If “auto”, the anchor is equivalent to “center” for data- referenced annotations or if there is an arrow, whereas for paper-referenced with no arrow, the anchor picked corresponds to the closest side.
即使没有实际的注释文本,(place-holder) 文本字段仍会呈现(默认情况下锚定到 'center'
),这就是推动箭头尾部的原因。将 xanchor 设置为 'right'
可确保箭头从预期位置开始,同时将空文本字段推到其左侧。