How do I create a multiline plot using seaborn?

Seaborn favors the “long format” as input. The key ingredient to convert your DataFrame from its “wide format” (one column per measurement type) into long format (one column for all measurement values, one column to indicate the type) is pandas.melt. Given a data_preproc structured like yours, filled with random values:

num_rows = 20
years = list(range(1990, 1990 + num_rows))
data_preproc = pd.DataFrame({
    'Year': years, 
    'A': np.random.randn(num_rows).cumsum(),
    'B': np.random.randn(num_rows).cumsum(),
    'C': np.random.randn(num_rows).cumsum(),
    'D': np.random.randn(num_rows).cumsum()})

A single plot with four lines, one per measurement type, is obtained with

sns.lineplot(x='Year', y='value', hue="variable", 
             data=pd.melt(data_preproc, ['Year']))

(Note that ‘value’ and ‘variable’ are the default column names returned by melt, and can be adapted to your liking.)

Leave a Comment