简体   繁体   English

Seaborn 线图 - 基于峰值的数据

[英]Seaborn lineplot - data based on peaks

I would like to colour my the lines of a seaborn lineplot, based on where the peak lies.我想根据峰值所在的位置为 seaborn 线图的线着色。 This is my current plot这是我目前的 plot 当前地块

As seen, some days the peak is in bin 4.72, whilst in others its 5.24 and 5.83.如图所示,有些日子峰值在 bin 4.72,而在其他日子,峰值在 5.24 和 5.83。 I would like to colour based on these peaks.我想根据这些峰值着色。 So for the plot below, it would have 3 colours, whilst maintaining the dates in the legend.所以对于下面的 plot,它将有 3 种颜色,同时保持图例中的日期。

This is my panda's dataframe, called select_bins这是我熊猫的dataframe,叫select_bins

               2.79  3.1  3.44  3.82  4.25  4.72  5.24  5.83  6.47  7.19  7.99  8.88
date                                                                           
20180527     1   28   101   270   694  1253  1134   528   106    10     0     0
20180603     0    0     0     3    12    26    82    45     5     0     0     0
20180611     2    7    34   137   317   341   410   179    48    10     1     0
20180617     2    6    13    52   130   133   161    74    23     4     0     0
20180625     0    2     1     9    14    34    47    53     9     0     0     0
20180626     5    1     1     5    18    50    72   101    28     2     0     0
20180628     2    0     0     2    21    41    87    78    16     0     0     0
20180705     1    1     0     2    18    32    63    61    27     7     0     0
20180709     2    0     3     6    31    56   107   139    52    12     1     0

This is the code to plot.这是 plot 的代码。 As you can see, I transpose the select_bins dataframe to plot如您所见,我将select_bins dataframe 转置为 plot

ax = sns.lineplot(data = select_bins.T, dashes=False, palette = 'rocket')
plt.show()

You could group your data and assign individual color palettes:您可以对数据进行分组并分配单独的调色板:

import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

select_bins = pd.read_csv("test.txt", sep="\s{2,}", engine="python", index_col="date")

#identify unique peaks
col_bin = select_bins.idxmax(axis=1)
unique_val = np.unique(col_bin)

#provide information for palettes
palettes = ["Reds", "Blues", "Greys"]

fig, ax = plt.subplots()

#plot subgroups with their palettes, providing still individual colors within the palette for each line
for uv, pal in zip (unique_val, palettes):
    sns.lineplot(data = select_bins[col_bin==uv].T, dashes=False, palette = pal, ax=ax)

plt.show()

Sample output:样品 output: 在此处输入图像描述

Alternatively, you could use different line styles for the groups but for this you have to reshape your data first from wide to long form .或者,您可以对组使用不同的行 styles 但为此您必须首先将数据从宽格式调整为长格式 And since we have to convert the date to a string anyhow, why not converting the x-values to numbers for a more realistic representation of the curves?既然我们无论如何都必须将日期转换为字符串,为什么不将 x 值转换为数字以更真实地表示曲线呢?

import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

select_bins = pd.read_csv("test.txt", sep="\s{2,}", engine="python", index_col="date")
#identify columns to plot
cols=select_bins.columns
#identify peaks
select_bins["col_bin"] = select_bins.idxmax(axis=1)

#reshape data for plotting
plot_df = select_bins.reset_index().melt(id_vars=["date", "col_bin"], value_vars=cols)
plot_df = plot_df.astype({"date": str, "variable": float})

fig, ax = plt.subplots(figsize=(10, 6))
sns.lineplot(data = plot_df, x="variable", y="value", hue="date", style="col_bin", palette = "rocket", ax=ax)
plt.xticks(ticks=[float(x) for x in cols], labels=cols)

plt.show()

Sample output:样品 output: ![在此处输入图像描述

If you create another variables in your dataframe, encoding a value for the peaks [1,2,3] for each line and then set this to hue :如果您在 dataframe 中创建另一个变量,为每行的峰值[1,2,3]编码一个值,然后将其设置为hue

sns.lineplot(data = select_bins.T, dashes=False, palette = 'rocket', hue="peak_encoding")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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