Search code examples
pythonpandasplotseaborn

Seaborn lineplot plot all entries (lines) separately using one grouping variable for coloring


I am trying to plot all the entries in a dataframe by using one variable for legend purposes. My dataframe looks like:

enter image description here

If I try to plot by sns.lineplot and hue='Punto', it aggregates all the entries.

ax = sns.lineplot(data = dfnew, x = 'Profundidad', y= 'OD', marker = 'o', hue='Punto')
ax.set(xlabel = 'Profundidad (m)',
       ylabel = 'Oxígeno disuelto (mg/L)',
       title = 'Oxígeno disuelto - Profundidad RCNP',
       xlim=(0.4, 3))
fig = plt.gcf()
fig.set_size_inches(10, 5)

enter image description here

I tried two different things: (1) Remove the estimator, but it connects the "ending" point of one day with the "starting" point of the next day.

ax = sns.lineplot(data = dfnew, x = 'Profundidad', y= 'OD', marker = 'o', hue='Punto', estimator=None, sort=False)
ax.set(xlabel = 'Profundidad (m)',
       ylabel = 'Oxígeno disuelto (mg/L)',
       title = 'Oxígeno disuelto - Profundidad RCNP',
       xlim=(0.4, 3))
fig = plt.gcf()
fig.set_size_inches(10, 5)

enter image description here

(2) Use two different grouping variables by using both hue and style (which plots what I want, but with different styles)

ax = sns.lineplot(data = dfnew, x = 'Profundidad', y= 'OD', marker = 'o', hue='Punto', style='Fecha')
ax.set(xlabel = 'Profundidad (m)',
       ylabel = 'Oxígeno disuelto (mg/L)',
       title = 'Oxígeno disuelto - Profundidad RCNP',
       xlim=(0.4, 3))
fig = plt.gcf()
fig.set_size_inches(10, 5)

enter image description here

In other words, I want the second plot, but using only the "first" legend (hue='Punto').

Can someone help me? Thank you very much!


Solution

  • Ok, I used a different approach, by using default pandas plot function (not as fast nor simple as I wanted, but it works).

    from matplotlib.lines import Line2D
    
    fig, ax = plt.subplots()
    colors = sns.color_palette()
    puntos = dfnew['Punto'].unique()
    for n, punto in enumerate(puntos):
      subset = dfnew[dfnew['Punto'] == punto]
      registros = subset['Fecha'].unique()
      c = colors[n]
      for registro in registros:
        subset[subset['Fecha'] == registro].plot(x = 'Profundidad', y = 'OD', marker = 'o', alpha=0.75, color = c, markeredgecolor = 'white', ax=ax)
    
    lines = [Line2D([0], [0], color=c, linewidth=2, alpha=0.75) for c in colors[:len(puntos)]]
    ax.legend(lines, puntos)
    ax.set_xlabel('Profundidad (m)')
    ax.set_ylabel('Oxígeno disuelto (mg/L)')
    ax.set_title('Oxígeno disuelto - Profundidad RCNP')
    fig.set_size_inches(10, 5)