Plotly:如何添加自定义图例

Plotly: How to add custom legend

我做了一个情节,我想添加一个带有一些文字的图例。

旧link(现已损坏)

https://plot.ly/~smirnod1/4/roc-curve/

编辑link 到类似情节:

https://plot.ly/~wonglynn2004/44/roc-plot-and-area-under-curve/#/code

这里有一个例子 - 一条 ROC 曲线,我想把 AUC 放在图例中,但是我找不到关于微调图例内容的文档。

这在一定程度上取决于您的图表类型和数据在图中的顺序。在与您在此处描述的情况类似的情况下,您可以通过编辑痕迹的 name 属性 来更改图例中显示的内容:

fig['data'][0]['name'] = 'ROC'
fig['data'][1]['name'] = 'AUC'

剧情:

代码:

import plotly.graph_objects as go
import numpy as np
#py.sign_in('username', 'api_key')

x1 = [0.0,
     0.012533802635532472,
     0.030175559084860713,
     0.05447053268661201,
     0.0876078465038417,
     0.12868609692235053,
     0.18101043052753574,
     0.23509464737949093,
     0.3178950079409366,
     0.41812250504356785,
     0.5606730480319354,
     0.8282182255225995]

y1 = [0.0,
     0.2159709618874773,
     0.338777979431337,
     0.4337568058076225,
     0.5160314579552329,
     0.5952813067150635,
     0.6657592256503327,
     0.7338173018753781,
     0.7997580157289776,
     0.8620689655172413,
     0.925892316999395,
     0.9824561403508771]

x2 = [0.0,
     0.010301755590848607,
     0.025368073142464694,
     0.044640940893677296,
     0.07138258144825514,
     0.10636562647551187,
     0.15169335107524573,
     0.21243078507962398,
     0.2978924325020389,
     0.43808215650083704,
     0.7974846546765678]

y2 = [0.00030248033877797946,
     0.25650332728372655,
     0.39231699939503933,
     0.49122807017543857,
     0.5795523290986085,
     0.6636418632788869,
     0.7380520266182699,
     0.8061101028433152,
     0.8702359346642469,
     0.9319419237749547,
     0.9879007864488808]

trace1 = {
        "uid": "aa384d16-8ecd-11e8-8006-023b3ac8d721", 
        "line": {
          "color": "darkorange", 
          "width": 2
        }, 
        "mode": "lines", 
        "name": "Baseline - 0.828", 
        "type": "scatter", 
        "x": x1,
        "y": y1
      }
trace2 = {
        "uid": "aa385018-8ecd-11e8-8006-023b3ac8d721", 
        "line": {
          "color": "darkorange", 
          "width": 2
        }, 
        "mode": "lines", 
        "name": "Refresh - 0.877", 
        "type": "scatter", 
        "x": x2, 
        "y": y2
      }
data = [trace1, trace2]
layout = {
        "title": "ROC Plot and Area Under Curve", 
        "width": 600, 
        "xaxis": {"title": "False Positive Rate"}, 
        "yaxis": {"title": "True Positive Rate"}, 
        "height": 500, 
        "legend": {
          "x": 0.8, 
          "y": 0.1
        }, 
        "autosize": False
      }
fig = go.Figure(data=data, layout=layout)

fig['data'][0]['name'] = 'ROC'
fig['data'][1]['name'] = 'AUC'

fig.show()