簡體   English   中英

Seaborn lineplot plot 所有條目(線)分別使用一個分組變量進行着色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM