1
votes

How can we specify dashed lines vs dotted lines through plotly's go.Layout for dataframe datasets? Below is my code which uses a dataframe and spits out a plot with solid lines.

I want to use a second dataframe object to have dashed lines in the same plot.

In some sense, I am trying to compare two dataframes which have the same exact column names but they need to have different line-formats for easier comparison.

P.S. help(go.Layout) only has information on spikedlines but not on usual line formats.

Many thanks,

init_notebook_mode(connected=True)
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd

#sample dataframe defined into `df1`

plot_df = [{
    'x': df1.index,
    'y': df1[col],
    'name': col
}  for col in df1.columns]

layout= go.Layout(
    title= 'Plot_with_solidline',
    xaxis= dict(title= 'Iteration'),
    yaxis=dict(title= 'Accuracy'),
    showlegend= True
)
fig= go.Figure(data=plot_df, layout=layout)
plot(fig, filename='pandas-line-naming-traces', auto_open=False)
1

1 Answers

1
votes

Figured out the solution and answering for others that may grapple with such errors.

The addition needs to be done in the data object itself.

init_notebook_mode(connected=True)
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd

#sample dataframe defined into `df1`, `df2`

layout=go.Layout(
    title= 'Plot_with_solidline_dashline',
    xaxis= dict(title= 'Iteration'),
    yaxis=dict(title= 'Accuracy'),
    showlegend= True
)
plot_df=[]
for col in df1.columns:
    plot_df.append(
        go.Scatter(x=df1.index, y=df1[col], mode='lines', line={'dash': 'solid'}, name=col)
    )
    plot_df.append(
        go.Scatter(x=df2.index, y=df2[col], mode='lines', line={'dash': 'dash'}, name=col)
    )

fig= go.Figure(data=plot_df, layout=layout)
plot(fig, filename='pandas-line-naming-traces', auto_open=False)