Python 如何在绘图本身之外注释点?

Python 如何在绘图本身之外注释点?,python,plot,plotly,Python,Plot,Plotly,我有一个这样的情节,想在上面加上“今天”的文字 我在尝试注释 annots.append(dict(x='2020-03-29',y=len(df)+1,text='<b>Today<b>', showarrow=False, font=dict(color='black'))) annots.append(dict(x='2020-03-29',y=len(df)+1,text='Today',showarrow=False,font=dict(color='bla

我有一个这样的情节,想在上面加上“今天”的文字

我在尝试注释

annots.append(dict(x='2020-03-29',y=len(df)+1,text='<b>Today<b>', showarrow=False, font=dict(color='black')))
annots.append(dict(x='2020-03-29',y=len(df)+1,text='Today',showarrow=False,font=dict(color='black'))
但是输出是这样的,我希望它在绘图上面,而不是改变绘图的结构!

您可以通过三个步骤完成此操作:

  • 使用
    fig.update\u布局(margin=dict())
  • 使用
    图添加线。添加_形()
  • 使用
    add_注释(yref='paper',y=1.06)
    在绘图外部添加文本,例如
    y>1
    将注释放置在绘图本身上方
  • 有关详细信息,请参见代码段

    绘图:

    # imports
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    from IPython.core.display import display, HTML
    import plotly.figure_factory as ff
    import plotly.graph_objs as go
    
    # setup
    display(HTML("<style>.container { width:50% !important; } .widget-select > select {background-color: gainsboro;}</style>"))
    init_notebook_mode(connected=True)
    
    #%qtconsole --style vim
    
    # dates
    StartA = '2009-01-01'
    StartB = '2009-03-05'
    StartC = '2009-02-20'
    
    FinishA='2009-02-28'
    FinishB='2009-04-15'
    FinishC='2009-05-30'
    
    LabelDateA='2009-01-25'
    LabelDateB='2009-03-20'
    LabelDateC='2009-04-01'
    
    # sample data
    df = [dict(Task="Task A", Start=StartA, Finish=FinishA),
          dict(Task="Task B", Start=StartB, Finish=FinishB),
          dict(Task="Task C", Start=StartC, Finish=FinishC)]
    
    # figure
    fig = ff.create_gantt(df)
    
    # add annotations
    annots =  [dict(x=LabelDateA,y=0,text="Task label A", showarrow=False, font=dict(color='white')),
               dict(x=LabelDateB,y=1,text="Task label B", showarrow=False, font=dict(color='White')),
               dict(x=LabelDateC,y=2,text="Task label C", showarrow=False, font=dict(color='White'))]
    
    # plot figure
    fig['layout']['annotations'] = annots
    
    
    # Step 1 - adjust margins to make room for the text
    fig.update_layout(margin=dict(t=150))
    
    # Step 2 - add line
    fig.add_shape(type='line',
                    x0=LabelDateB,
                    y0=0,
                    x1=LabelDateB,
                    y1=1,
                    line=dict(color='black', dash='dot'),
                    xref='x',
                    yref='paper'
    )
    
    # Step 3 - add text with xref set to x
    # and yref set to 'paper' which will let you set
    # text outside the plot itself by specifying y > 1
    fig.add_annotation(dict(font=dict(color="black",size=12),
                                #x=x_loc,
                                x=LabelDateB,
                                y=1.06,
                                showarrow=False,
                                text='<b>Today</b>',
                                textangle=0,
                                xref="x",
                                yref="paper"
                               ))
    
    fig.update_layout(
        title_text="Academic year 2019/2020"
    )
    
    fig.show()
    

    完整代码:

    # imports
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    from IPython.core.display import display, HTML
    import plotly.figure_factory as ff
    import plotly.graph_objs as go
    
    # setup
    display(HTML("<style>.container { width:50% !important; } .widget-select > select {background-color: gainsboro;}</style>"))
    init_notebook_mode(connected=True)
    
    #%qtconsole --style vim
    
    # dates
    StartA = '2009-01-01'
    StartB = '2009-03-05'
    StartC = '2009-02-20'
    
    FinishA='2009-02-28'
    FinishB='2009-04-15'
    FinishC='2009-05-30'
    
    LabelDateA='2009-01-25'
    LabelDateB='2009-03-20'
    LabelDateC='2009-04-01'
    
    # sample data
    df = [dict(Task="Task A", Start=StartA, Finish=FinishA),
          dict(Task="Task B", Start=StartB, Finish=FinishB),
          dict(Task="Task C", Start=StartC, Finish=FinishC)]
    
    # figure
    fig = ff.create_gantt(df)
    
    # add annotations
    annots =  [dict(x=LabelDateA,y=0,text="Task label A", showarrow=False, font=dict(color='white')),
               dict(x=LabelDateB,y=1,text="Task label B", showarrow=False, font=dict(color='White')),
               dict(x=LabelDateC,y=2,text="Task label C", showarrow=False, font=dict(color='White'))]
    
    # plot figure
    fig['layout']['annotations'] = annots
    
    
    # Step 1 - adjust margins to make room for the text
    fig.update_layout(margin=dict(t=150))
    
    # Step 2 - add line
    fig.add_shape(type='line',
                    x0=LabelDateB,
                    y0=0,
                    x1=LabelDateB,
                    y1=1,
                    line=dict(color='black', dash='dot'),
                    xref='x',
                    yref='paper'
    )
    
    # Step 3 - add text with xref set to x
    # and yref set to 'paper' which will let you set
    # text outside the plot itself by specifying y > 1
    fig.add_annotation(dict(font=dict(color="black",size=12),
                                #x=x_loc,
                                x=LabelDateB,
                                y=1.06,
                                showarrow=False,
                                text='<b>Today</b>',
                                textangle=0,
                                xref="x",
                                yref="paper"
                               ))
    
    fig.update_layout(
        title_text="Academic year 2019/2020"
    )
    
    fig.show()
    
    #导入
    从plotly.offline导入下载\u plotlyjs,初始化\u笔记本\u模式,绘图,iplot
    从IPython.core.display导入显示,HTML
    将plotly.figure\u工厂作为ff导入
    导入plotly.graph_objs作为go
    #设置
    显示(HTML(“.container{width:50%!important;}.widget select>select{background color:gainsboro;}”))
    初始笔记本模式(已连接=真)
    #%qtconsole——风格vim
    #日期
    StartA='2009-01-01'
    StartB='2009-03-05'
    StartC='2009-02-20'
    FinishA='2009-02-28'
    完成日期class='2009-04-15'
    完成时间class='2009-05-30'
    LabelDateA='2009-01-25'
    LabelDateB='2009-03-20'
    LabelDateC='2009-04-01'
    #样本数据
    df=[dict(Task=“Task A”,Start=StartA,Finish=FinishA),
    dict(Task=“Task B”,Start=StartB,Finish=FinishB),
    dict(Task=“Task C”,Start=StartC,Finish=FinishC)]
    #身材
    图=ff.创建甘特图(df)
    #添加注释
    annots=[dict(x=LabelDateA,y=0,text=“任务标签A”,showarrow=False,font=dict(color='white')),
    dict(x=LabelDateB,y=1,text=“任务标签B”,showarrow=False,font=dict(color='White')),
    dict(x=LabelDateC,y=2,text=“任务标签C”,showarrow=False,font=dict(color='White'))]
    #绘图
    图['layout']['annotations']=annots
    #第1步-调整边距,为文本腾出空间
    图更新布局(边距=dict(t=150))
    #步骤2-添加行
    图添加_形(type='line',
    x0=LabelDateB,
    y0=0,
    x1=LabelDateB,
    y1=1,
    line=dict(color='black',dash='dot'),
    xref='x',
    yref='paper'
    )
    #步骤3-添加外部参照设置为x的文本
    #yref设置为“纸”,这将允许您设置
    #通过指定y>1,文本位于打印本身之外
    图添加注释(dict(font=dict(color=“black”,size=12),
    #x=x_loc,
    x=LabelDateB,
    y=1.06,
    showarrow=False,
    text='Today',
    textangle=0,
    xref=“x”,
    yref=“纸张”
    ))
    图1.2.2.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1(
    title_text=“2019/2020学年”
    )
    图2(图3)