简体   繁体   English

如何在 Seaborn 的误差带中绘制所有线?

[英]How do I plot all the lines in the error band in Seaborn?

I use this to create a line plot with error bands similar to this: https://seaborn.pydata.org/examples/errorband_lineplots.html我用它来创建一个带有与此类似的错误带的线图: https ://seaborn.pydata.org/examples/errorband_lineplots.html

g = sns.lineplot(data=df, x="ann_num_parts", y="probability", hue="full_ann_num_parts", palette=sns.color_palette("hls", 6), err_style="band")

However, I want to plot the actual lines in the error band, ie every row in the data, with a lighter color (lower alpha) than the line of the mean.但是,我想绘制误差带中的实际线,即数据中的每一行,颜色比平均值线更浅(低 alpha)。 How do I do this?我该怎么做呢? I'm pretty lost, so any high-level idea or relevant documentation would help.我很迷茫,所以任何高级想法或相关文档都会有所帮助。 Thanks!谢谢!

Since there is no data presented, I have taken a sample from the official reference and applied your code.由于没有提供数据,我从官方参考中抽取了一个样本并应用了您的代码。 The customization of the error bands cannot be achieved using only Seaborn, so I use matplotlib to make the changes.仅使用 Seaborn 无法实现误差带的自定义,因此我使用 matplotlib 进行更改。 Set any color for polly collection and line2D.为 polly 集合和 line2D 设置任何颜色。 At the same time, set the alpha value to 1.0 for the polly collection, since the alpha value is set low as per default.同时,将 polly 集合的 alpha 值设置为 1.0,因为默认情况下 alpha 值设置为低。 In addition, the legend handler is still in its original color, so create a line of the specified color and set it to the legend.此外,图例处理程序仍然是其原始颜色,因此创建一条指定颜色的线并将其设置为图例。 The manipulation of objects in matplotlib could be written more smartly. matplotlib 中对象的操作可以写得更巧妙。

import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from matplotlib.lines import Line2D
import seaborn as sns
sns.set_theme(style="darkgrid")

custom_lines = [Line2D([0], [0], color='#ffc0cb', lw=2), Line2D([0], [0], color='#00ffff', lw=2)]

# Load an example dataset with long-form data
fmri = sns.load_dataset("fmri")

# Plot the responses for different events and regions
g = sns.lineplot(x="timepoint", y="signal",
                 hue="event", data=fmri,
                 palette=sns.color_palette("hls", 2), err_style="band")
#print(g.get_children())

colors = ['#ff0000','#ff0000','#1e90ff','#1e90ff']
colors2 = ['#ffc0cb','#00ffff','#ffc0cb','#00ffff']
i = 0
for p in g.get_children():
    if type(p).__name__ == 'PolyCollection':
        #print(p.get_facecolor())
        p.set_color(colors[i])
        p.set_alpha(1.0)
    if type(p).__name__ == 'Line2D':
        #print(p.get_color())
        p.set_color(colors2[i])
        i += 1
_, labels = g.get_legend_handles_labels()
g.legend(custom_lines, labels)
plt.show()

在此处输入图像描述

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

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