在图纸区域向x轴添加箭头,返回错误

2024-09-29 22:03:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在x_轴下方绘制一个箭头,代码如下:


       myplot = go.Figure(data=go.Scatter(x=df['time'], y=df['count'], mode='markers'))
        

        xstart = -2
        xmax = 3.5
        xmin = -3.5
        padding = 0.05
        ypos = -0.1


        mylplot.update_layout(
            plot_bgcolor=colors['backgraph'],
            paper_bgcolor=colors['backpaper'],
            font_color=colors['text'],
            showlegend=False,
            width=550,
            height=550,
            xaxis=dict(title_text="Time", showgrid=False, showticklabels=False),
            yaxis=dict(title_text="Counts", showgrid=False, showticklabels=False),
            
            annotations=[dict(
                x=xmin,
                y=ypos,
                ax=xstart + padding,
                ay=ypos,
                xref='x',
                axref='x',
                yref='paper',
                ayref='paper',
                showarrow=True,
                arrowhead=2,
                arrowsize=1,
                arrowwidth=3,
                arrowcolor='#0000ff', 
            )],

但我收到以下错误:

ValueError: 
    Invalid value of type 'builtins.str' received for the 'ayref' property of layout.annotation
        Received value: 'paper'

    The 'ayref' property is an enumeration that may be specified as:
      - One of the following enumeration values:
            ['pixel']
      - A string that matches one of the following regular expressions:
            ['^y([2-9]|[1-9][0-9]+)?$']```

如果将ayref设置为y,则不显示箭头

我试图运行this example,但出现了相同的错误


Tags: ofthetextfalsegodf箭头dict
1条回答
网友
1楼 · 发布于 2024-09-29 22:03:44

我能够实现使用标题文本。对于希望在plotly中构建象限的用户来说,这也是一个很好的示例:

 quadplot = px.scatter(df, x="time", y ="count", hover_name="name", color="status", hover_data="name", color_discrete_map=color_discrete_map)

#there is a small offset adjustment in order to be in the center.
xm = (df['time'].max()/2) + ((df['time'].max()/100)*3) 
ym = (df['count'].max()/2) + ((df['count'].max()/100)*3)
xf = df['time'].max()
yf = df['count'].max()


quadplot.update_layout(
            plot_bgcolor=colors['b'],
            paper_bgcolor=colors['ba'],
            font_color=colors['text'],
            showlegend=False,
            width=550,
            height=550,
            xaxis=dict(title_text="(-) «───────── Time ─────────» (+)", showgrid=False, showticklabels=False, rangemode='tozero', mirror=True, linecolor=colors['line'], linewidth=2),
            yaxis=dict(title_text="(-) «───────── Count ─────────» (+)", showgrid=False, showticklabels=False, rangemode='tozero', mirror=True, linecolor=colors['line'], linewidth=2),
            shapes=[dict(
                type='line',
                yref = 'paper', y0=0, y1=1,
                xref = 'x', x0=xm, x1=xm,
                line=dict(
                    color=colors['line'],
                    width=2,
                    ),
                )])

  quadplot.add_shape(
            type='line',
            yref = 'y', y0=ym, y1=ym,
            xref = 'paper', x0=0, x1=1,
            line=dict(
                color=colors['line'],
                width=2,
                ))

        

相关问题 更多 >

    热门问题