Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 506
Dmitry Smirnov Avatar asked Jan 11 '16 17:01

Dmitry Smirnov


People also ask

How do you change the legend size in plotly?

Plotly's update_layout() function is used to change legend size in the plotly chart. The values in the input dict / keyword arguments are used to iteratively alter the parts of the original layout.

Is plotly customizable?

Figures made with Plotly Express can be customized in all the same ways as figures made with graph objects, as well as with PX-specific function arguments. New to Plotly? Plotly is a free and open-source graphing library for Python.

What is the difference between plotly and Plotly Express?

The plotly.express module (usually imported as px ) contains functions that can create entire figures at once, and is referred to as Plotly Express or PX. Plotly Express is a built-in part of the plotly library, and is the recommended starting point for creating most common figures.


1 Answers

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:

enter image description here

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()
like image 166
vestland Avatar answered Sep 27 '22 17:09

vestland