简体   繁体   中英

Plotly: How to add custom legend

I've made a plot, and I want to add a legend with some text in it.

Old link (now broken)

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

Edited link to similar plot:

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

Here is one example - a ROC curve, and I would like to put AUC in the legend, but I can't find documentation on fine-tuning the legend contents.

在此处输入图像描述

This will depend a bit on your chart type and the order in which your data is included in the figure. In a similar case to what you're describing here, you can change what's displayed in the legend by editing the name property of your traces:

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

Plot:

在此处输入图像描述

Code:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM