[英]Seaborn lineplot plot all entries (lines) separately using one grouping variable for coloring
我正在尝试 plot dataframe 中的所有条目,方法是使用一个变量用于图例目的。 我的 dataframe 看起来像:
如果我尝试通过 sns.lineplot 和 hue='Punto' 来 plot,它会聚合所有条目。
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)
我尝试了两种不同的方法:(1)删除估计器,但它将一天的“结束”点与第二天的“开始”点连接起来。
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)通过使用色调和风格来使用两个不同的分组变量(绘制我想要的,但风格不同)
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)
换句话说,我想要第二个 plot,但只使用“第一个”图例(hue='Punto')。
有人能帮我吗? 非常感谢!
好的,我使用了另一种方法,使用默认的 pandas plot function (不像我想要的那么快也不简单,但它可以工作)。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.