imantha
imantha

Reputation: 3838

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

enter image description here

Upvotes: 0

Views: 969

Answers (1)

Redox
Redox

Reputation: 10017

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

enter image description here

Upvotes: 1

Related Questions