简体   繁体   中英

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:

在此处输入图像描述

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)

在此处输入图像描述

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)

在此处输入图像描述

(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)

在此处输入图像描述

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

Can someone help me? Thank you very much!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM