从点到绘图轴的线

Lines from points to plot axes

请告诉我!如图所示,是否有可能以某种方式向指向图表轴的点添加线条?当然,您可以手动完成。我搜索了文档但没有找到

Plotly 中没有任何东西可以自动添加这样的注释。但是,您可以通过创建一个函数来以动态方式手动执行此操作,该函数接收要添加垂直线和水平线的每个点。

下面我重新创建了一个与你显示的类似的分段函数(逻辑+水平线段),隐藏了yaxis,并使用注释在图中x=0处显示yaxis,并添加了水平和每个兴趣点的垂直线。

import numpy as np
from math import e
import plotly.graph_objects as go

fig = go.Figure()

## reproduce your piecewise function
x1 = np.linspace(-1,1,500)
y1 = 1 / (1 + e**(2.5*(1.5-x1)))

x2 = np.linspace(1,1.5,500)
y2 = [0.5]*500

x3 = np.linspace(1.5,3,500)
y3 = 1 / (1 + e**(5*(1.0-x3)))

fig.add_trace(go.Scatter(x=x1,y=y1,line=dict(color="red")))
fig.add_trace(go.Scatter(x=[1],y=[0.5],mode='markers',line=dict(color="red")))
fig.add_trace(go.Scatter(x=x2,y=y2,line=dict(color="red")))
fig.add_trace(go.Scatter(x=[x3[0]],y=[y3[0]],mode='markers',line=dict(color="red")))
fig.add_trace(go.Scatter(x=x3,y=y3,line=dict(color="red")))
fig.update_layout(template="simple_white", showlegend=False)

## remove yaxis but set the range
fig.update_yaxes(range=[0, 1.1], visible=False)

## add custom yaxis at x=0
fig.add_vline(x=0, line_width=1, line_color="black")
for y_tick in np.linspace(0,1,6):
    fig.add_trace(go.Scatter(
        x=[-0.01,0.01],
        y=[y_tick]*2,
        mode="lines+text",
        name=None,
        hoverinfo='none',
        text=[f'{y_tick:.1f}'],
        textposition="top left",
        line=dict(color="black", width=1)
    ))

## function to add horizontal and vertical lines for points
def add_horizontal_and_vertical_lines(fig, x_val, y_val):
    fig.add_trace(go.Scatter(
        x=[x_val]*2,
        y=[0,y_val],
        mode="lines",
        hoverinfo='none',
        line=dict(color="grey", width=0.5)
    ))
    fig.add_trace(go.Scatter(
        x=[0,x_val],
        y=[y_val]*2,
        mode="lines",
        hoverinfo='none',
        line=dict(color="grey", width=0.5)
    ))

## loop through the points of interest
for x_point,y_point in zip([x1[-1],x1[-1],x2[-1],x2[-1],x3[-1]],[y1[-1],y2[0],y2[-1],y3[0],y3[-1]]):
    add_horizontal_and_vertical_lines(fig, x_point, y_point)

fig.show()