【问题标题】:How to annotate subplots in Plotly inside a for loop如何在 for 循环内的 Plotly 中注释子图
【发布时间】:2019-01-09 07:03:02
【问题描述】:

我正在尝试在 for 循环中注释我的子图。每个子图将在图上打印 RMS 值。我尝试通过以下方式做到这一点:

from plotly import tools
figg = tools.make_subplots(rows=4, cols=1)

fake_date = {"X":   np.arange(1, 101, 0.5), "Y": np.sin(x), "Z": [x + 1 for x in range(10)] * 20}
fake_date = pd.DataFrame(fake_date)
fake_date.sort_values("Z")

unique_ids = fake_date['Z'].unique()
train_id, test_id = np.split(np.random.permutation(unique_ids), [int(.6 * len(unique_ids))])


for i, j in enumerate(test_id):

    x_test = fake_date[fake_date['Z'].isin([test_id[i]])] 
    y_test = fake_date[fake_date['Z'].isin([test_id[i]])]


    # Evaluate 
    rms_test = 0.04
    r_test = 0.9



    Real = {'type' : 'scatter',
                     'x' : x_test.X,
                     'y' : x_test.Y,
                "mode" : 'lines+markers', 
                "name" : 'Real'}




    figg.append_trace(Real, i+1, 1)


figg['layout'].update( annotations=[dict(x = 10,y = 0.2,  text= rms_test, xref= "x1",yref="y1")]  )
figg['layout'].update(height=1800, width=600, title='Testing')
pyo.iplot(figg)

这不起作用,尽管here 给出的答案似乎对其他人有用。谁能指出我做错了什么? 我为重现性生成了假日期

【问题讨论】:

    标签: pandas plotly subplot


    【解决方案1】:

    我不确定 RMS 值的确切位置,但下面是一个示例代码,可帮助您实现所需。

    我们创建一个数组annotation_arr,我们使用for循环在其中存储注释。

    我们需要为每个单独的轴设置xvalyval。请记住,第一个轴是x,第二个是x2,所以我为此写了一个三元条件,请查看下面的代码,如果有任何问题,请告诉我!

    import plotly.graph_objs as go
    from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
    from plotly import tools
    import numpy as np
    import pandas as pd
    init_notebook_mode(connected=True)
    rows = 4
    figg = tools.make_subplots(rows=rows, cols=1)
    
    fake_date = {"X":   np.arange(0, 100, 0.5), "Y": [np.sin(x) for x in range(200)], "Z": [x + 1 for x in range(10)] * 20}
    fake_date = pd.DataFrame(fake_date)
    fake_date.sort_values("Z")
    
    unique_ids = fake_date['Z'].unique()
    train_id, test_id = np.split(np.random.permutation(unique_ids), [int(.6 * len(unique_ids))])
    top = 0
    annotation_arr = []
    for i, j in enumerate(test_id):
    
        x_test = fake_date[fake_date['Z'].isin([test_id[i]])] 
        y_test = fake_date[fake_date['Z'].isin([test_id[i]])]
    
    
        # Evaluate 
        rms_test = 0.04
        r_test = 0.9
    
    
    
        Real = {'type' : 'scatter',
                         'x' : x_test.X,
                         'y' : x_test.Y,
                    "mode" : 'lines+markers', 
                    "name" : 'Real'}
    
    
        top = top + 1/rows
        i_val = "" if i == 0 else i + 1
        annotation_arr.append(dict(x = r_test,y = top,  text= rms_test, xref= "x"+str(i_val),yref="y"+str(i_val)))
        figg.append_trace(Real, i+1, 1)
    
    
    figg['layout'].update( annotations=annotation_arr  )
    figg['layout'].update(height=1800, width=600, title='Testing')
    iplot(figg)
    

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 2017-05-29
      • 1970-01-01
      • 2021-10-28
      • 2013-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      相关资源
      最近更新 更多