猿问

文本位置不显示在情节上

我正在尝试使用 plotly 绘制我的神经网络的训练和测试集的准确性。我还想添加一个带有文本的标记,该文本说明每个时间的最大值是什么时候,但还显示一个说明该值是什么的文本。我尝试在这个例子中做类似的事情。


这是我的mcve:


import plotly.graph_objects as go


data = {

    'test acc':  [1, 2, 3, 4, 5, 6, 7, 9, 10],

    'train acc': [3, 5, 5, 6, 7, 8, 9, 10, 8]

}


fig = go.Figure()

color_train = 'rgb(255, 0, 0)'

color_test = 'rgb(0, 255, 0)'

assert len(data["train acc"]) == len(data["test acc"])

x = list(range(len(data["train acc"])))

fig.add_trace(go.Scatter(x=x,

                         y=data["train acc"],

                         mode='lines',

                         name='train acc',

                         line_color=color_train))

fig.add_trace(go.Scatter(x=x,

                         y=data["test acc"],

                         mode='lines',

                         name='test acc',

                         line_color=color_test))

# Max points

train_max = max(data["train acc"])

test_max = max(data["test acc"])

# ATTENTION! this will only give you first occurrence

train_max_index = data["train acc"].index(train_max)

test_max_index = data["test acc"].index(test_max)


fig.add_trace(go.Scatter(x=[train_max_index],

                         y=[train_max],

                         mode='markers',

                         name='max value train',

                         text=['{}%'.format(int(train_max * 100))],

                         textposition="top center",

                         marker_color=color_train))

fig.add_trace(go.Scatter(x=[test_max_index],

                         y=[test_max],

                         mode='markers',

                         name='max value test',

                         text=['{}%'.format(int(test_max*100))],

                         textposition="top center",

                         marker_color=color_test))


fig.update_layout(title='Train vs Test accuracy',

                  xaxis_title='epochs',

                  yaxis_title='accuracy (%)'

                  )

fig.show()

但是,我的输出火力如下:

如您所见,该值没有像我找到的示例那样显示。我怎样才能让它出现?



慕标琳琳
浏览 95回答 1
1回答

慕无忌1623718

如果您只想突出显示某些特定值,请使用add_annotation(). 在您的情况下,只需找到您想要关注的 X 的最大和最小 Y。缺少您身边的数据样本,以下是我使用通用数据样本的方法:阴谋:代码:import plotly.graph_objects as goimport plotly.io as piopio.renderers.default='browser'fig = go.Figure()xVars1=[0, 1, 2, 3, 4, 5, 6, 7, 8]yVars1=[0, 1, 3, 2, 4, 3, 4, 6, 5]xVars2=[0, 1, 2, 3, 4, 5, 6, 7, 8]yVars2=[0, 4, 5, 1, 2, 2, 3, 4, 2]fig.add_trace(go.Scatter(    x=xVars1,    y=yVars1))fig.add_trace(go.Scatter(    x=xVars2,    y=yVars2))fig.add_annotation(            x=yVars1.index(max(yVars1)),            y=max(yVars1),            text="yVars1 max")fig.add_annotation(            x=yVars2.index(max(yVars2)),            y=max(yVars2),            text="yVars2 max")fig.update_annotations(dict(            xref="x",            yref="y",            showarrow=True,            arrowhead=7,            ax=0,            ay=-40))fig.update_layout(showlegend=False)fig.show()
随时随地看视频慕课网APP

相关分类

Python
我要回答