How to add arrows below the original axis in plotly 2D scatter plot

梦想与她 提交于 2021-02-08 10:04:34

问题


Creating 2D scatter plot by plotly can be done following the example from:

https://plot.ly/python/line-and-scatter/

However, is there any easy way to add arrows below the x axis?


回答1:


You could use Plotly's annotations without text.

import plotly
import numpy as np

plotly.offline.init_notebook_mode()
N = 1000

data = [plotly.graph_objs.Scatter(x=np.random.randn(N),
                                  y=np.random.randn(N),
                                  mode = 'markers'
                                 )
       ]

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

layout = plotly.graph_objs.Layout(
    xaxis=dict(range=[xmin, xmax]),
    showlegend=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',
        ),
        dict(
            x=xmax,
            y=ypos,
            ax=xstart - padding,
            ay=ypos,
            xref='x',
            axref='x',
            yref='paper',
            ayref='paper',
            showarrow=True,
            arrowhead=2,
            arrowsize=1,
            arrowwidth=3,
            arrowcolor='#ff0000',
        )
    ])

plotly.offline.iplot(plotly.graph_objs.Figure(data=data, 
                                              layout=layout))


来源:https://stackoverflow.com/questions/45575978/how-to-add-arrows-below-the-original-axis-in-plotly-2d-scatter-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!