Ben Chuanlong Du's Blog

It is never too late to learn.

Multiple Lines on the Same Plot Using hvplot

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [1]:
!curl -sSL -o loss.csv https://github.com/dclong/blog/files/7229259/loss.csv
In [5]:
import pandas as pd
import hvplot.pandas
In [4]:
df = pd.read_csv("loss.csv")
df
Out[4]:
Step Value_test Value_train
0 1 1.226414e-04 1.485743e-04
1 2 2.588446e-05 2.798802e-05
2 3 9.192328e-06 9.950256e-06
3 4 4.610128e-06 5.227189e-06
4 5 2.639660e-06 2.959981e-06
... ... ... ...
115 116 1.307412e-07 1.427061e-07
116 117 2.098449e-07 2.273528e-07
117 118 1.127275e-07 1.189315e-07
118 119 1.751949e-07 1.754398e-07
119 120 2.097909e-07 2.269361e-07

120 rows × 3 columns

Manually Overlay Multiple Plots

This way is good for creating subplots but is not suggested for overlaying multiple plots into one plot as you will have to do extra work to label lines.

In [8]:
df.hvplot.line("Step", "Value_train") + df.hvplot.line("Step", "Value_train")
Out[8]:
In [9]:
df.hvplot.line("Step", "Value_train") * df.hvplot.line("Step", "Value_train")
Out[9]:

groupby + overlay

This approach is best for slim-and-tall data.

In [14]:
df2 = df.melt(id_vars="Step", value_vars=["Value_train", "Value_test"])
df2
Out[14]:
Step variable value
0 1 Value_train 1.485743e-04
1 2 Value_train 2.798802e-05
2 3 Value_train 9.950256e-06
3 4 Value_train 5.227189e-06
4 5 Value_train 2.959981e-06
... ... ... ...
235 116 Value_test 1.307412e-07
236 117 Value_test 2.098449e-07
237 118 Value_test 1.127275e-07
238 119 Value_test 1.751949e-07
239 120 Value_test 2.097909e-07

240 rows × 3 columns

In [16]:
df2.hvplot.line("Step", "value", groupby="variable")
Out[16]:
In [18]:
df2.hvplot.line("Step", "value", groupby="variable").overlay()
Out[18]:

Direct Specify Multiple y-Columns

In [19]:
df.hvplot.line("Step", ("Value_train", "Value_test"))
Out[19]:
In [ ]:
 

Comments