Plotting a line chart across columns

I want to plot 10 lines in one Pyplot figure each representing a percentile of a specific column in my dataframe. The y-values for each line will come from 6 different columns.

I have grouped my dataframe with pd.qcut and manually done what I wanted this way:


fig3 = go.Figure()

x_values = ["9:35","9:45","10:00","11:00","13:00","15:00"]

fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[0,"Change935/Gap"]),df_PreVolume_grouped.loc[0,"Change945/Gap"],df_PreVolume_grouped.loc[0,"Change1000/Gap"],df_PreVolume_grouped.loc[0,"Change1100/Gap"],df_PreVolume_grouped.loc[0,"Change1300/Gap"],df_PreVolume_grouped.loc[0,"Change1500/Gap"]],name="1st Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[1,"Change935/Gap"]),df_PreVolume_grouped.loc[1,"Change945/Gap"],df_PreVolume_grouped.loc[1,"Change1000/Gap"],df_PreVolume_grouped.loc[1,"Change1100/Gap"],df_PreVolume_grouped.loc[1,"Change1300/Gap"],df_PreVolume_grouped.loc[1,"Change1500/Gap"]],name="2nd Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[2,"Change935/Gap"]),df_PreVolume_grouped.loc[2,"Change945/Gap"],df_PreVolume_grouped.loc[2,"Change1000/Gap"],df_PreVolume_grouped.loc[2,"Change1100/Gap"],df_PreVolume_grouped.loc[2,"Change1300/Gap"],df_PreVolume_grouped.loc[2,"Change1500/Gap"]],name="3rd Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[3,"Change935/Gap"]),df_PreVolume_grouped.loc[3,"Change945/Gap"],df_PreVolume_grouped.loc[3,"Change1000/Gap"],df_PreVolume_grouped.loc[3,"Change1100/Gap"],df_PreVolume_grouped.loc[3,"Change1300/Gap"],df_PreVolume_grouped.loc[3,"Change1500/Gap"]],name="4th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[4,"Change935/Gap"]),df_PreVolume_grouped.loc[4,"Change945/Gap"],df_PreVolume_grouped.loc[4,"Change1000/Gap"],df_PreVolume_grouped.loc[4,"Change1100/Gap"],df_PreVolume_grouped.loc[4,"Change1300/Gap"],df_PreVolume_grouped.loc[4,"Change1500/Gap"]],name="5th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[5,"Change935/Gap"]),df_PreVolume_grouped.loc[5,"Change945/Gap"],df_PreVolume_grouped.loc[5,"Change1000/Gap"],df_PreVolume_grouped.loc[5,"Change1100/Gap"],df_PreVolume_grouped.loc[5,"Change1300/Gap"],df_PreVolume_grouped.loc[5,"Change1500/Gap"]],name="6th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[6,"Change935/Gap"]),df_PreVolume_grouped.loc[6,"Change945/Gap"],df_PreVolume_grouped.loc[6,"Change1000/Gap"],df_PreVolume_grouped.loc[6,"Change1100/Gap"],df_PreVolume_grouped.loc[6,"Change1300/Gap"],df_PreVolume_grouped.loc[6,"Change1500/Gap"]],name="7th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[7,"Change935/Gap"]),df_PreVolume_grouped.loc[7,"Change945/Gap"],df_PreVolume_grouped.loc[7,"Change1000/Gap"],df_PreVolume_grouped.loc[7,"Change1100/Gap"],df_PreVolume_grouped.loc[7,"Change1300/Gap"],df_PreVolume_grouped.loc[7,"Change1500/Gap"]],name="8th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[8,"Change935/Gap"]),df_PreVolume_grouped.loc[8,"Change945/Gap"],df_PreVolume_grouped.loc[8,"Change1000/Gap"],df_PreVolume_grouped.loc[8,"Change1100/Gap"],df_PreVolume_grouped.loc[8,"Change1300/Gap"],df_PreVolume_grouped.loc[8,"Change1500/Gap"]],name="9th Percentile"))
fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[9,"Change935/Gap"]),df_PreVolume_grouped.loc[9,"Change945/Gap"],df_PreVolume_grouped.loc[9,"Change1000/Gap"],df_PreVolume_grouped.loc[9,"Change1100/Gap"],df_PreVolume_grouped.loc[9,"Change1300/Gap"],df_PreVolume_grouped.loc[9,"Change1500/Gap"]],name="10th Percentile"))


Since this is a little primitive, I’m curious how else it could be done. I’m not sure how to tell Plotly to use specific columns without typing out each value.

Bonus question: each time I call add_trace in Jupyter Notebook, the figure is plotted - currently I have 10 figures - the last one has 10 lines as I wanted. How do I avoid this?

Hi @plottingly

You could just loop through your add_trace function.

for i in range(0, 10):
    fig3.add_trace(go.Scatter(x = x_values, y=[(df_PreVolume_grouped.loc[i,"Change935/Gap"]),df_PreVolume_grouped.loc[i,"Change945/Gap"],df_PreVolume_grouped.loc[i,"Change1000/Gap"],df_PreVolume_grouped.loc[i,"Change1100/Gap"],df_PreVolume_grouped.loc[i,"Change1300/Gap"],df_PreVolume_grouped.loc[i,"Change1500/Gap"]],name="{0}st Percentile".format(str(i))

1 Like

Right, thanks. Definitely better than typing each line manually.

Curious though if there’s yet another way to do it.