简体   繁体   中英

Providing a label name in plotly express

Hi I am wondering how to give a label name when using plotly express where the data is not in the form of a DataFrame . Its not possible to use the keyword argument name when using px.line . However it is a possible when using px.scatter function.

As a result in the example below I end up with a legend name only for the second trace.

I am aware this can be easily done using plotly.graph_objs but wondering how to do in plotly.express

import plotly.express as px 
history_dict = history.history

p1 = px.line(
    x = np.arange(0,20),
    y = history_dict["loss"],
    # error if name keyword argument used here.
    title = "Training and Validation Loss",   
)

p1.add_scatter(
    x = np.arange(0,20),
    y = history_dict["val_loss"],
    mode = "lines",
    name = "val loss"
)

p1.update_layout(xaxis_title = "epochs", yaxis_title = "loss")

在此处输入图像描述

Once you have drawn the first line plot, you need to specify that you would like to display the legend and the name you want to provide. Add these lines between px.line() and p1.add_scatter()

p1['data'][0]['showlegend']=True
p1['data'][0]['name']='Loss'

Plot

在此处输入图像描述

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