简体   繁体   English

更改 Seaborn 显示中的属性

[英]Changing properties in Seaborn displot

It looks like a pretty straightforward task but I cannot find the right solution.这看起来是一项非常简单的任务,但我找不到正确的解决方案。 I do generate 10 collections with 100 samples in each and pack them into pandas.DataFrame assigning index numbers for each column.我确实生成了 10 个 collections,每个样本有 100 个样本,并将它们打包到pandas.DataFrame中,为每列分配索引号。

How to plot all 10 lines with: same color='b' for all, set alpha=0.5, linewidth=1.0?如何 plot 所有 10 行:所有 10 行相同颜色='b',设置 alpha=0.5,线宽=1.0?

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
import random


sns.set_style("darkgrid")
sns.set_context("talk")
SEED = 202107
np.random.seed(SEED)
random.seed(SEED)


Num = 10
val = [np.random.randn() for _ in range(Num)]
my_data = [mu + np.random.randn(100) for mu in val]

df = pd.DataFrame()
for index in range(Num):
    name = str(index)
    df[name] = my_data[index][:]

sns.displot(df, kind='kde', legend=False)


plt.show()

The answer requires realizing that in wide-form mode , displot assigns the column dimension to a hue variable.答案需要意识到在宽格式模式下displot将列维度分配给hue变量。 So you control the color through the palette parameter and the easiest way to make everything one color is to pass a list of identical entries with the right length.因此,您可以通过palette参数控制颜色,而使所有内容都变成一种颜色的最简单方法是传递具有正确长度的相同条目列表。 The other properties can be controlled by matplotlib keyword arguments that get passed through to plt.plot :其他属性可以通过传递给plt.plot的 matplotlib 关键字 arguments 来控制:

g = sns.displot(
    df, kind='kde', legend=False,
    palette=["C0"] * Num, linewidth=1
)
plt.setp(g.ax.lines, alpha=.5)

在此处输入图像描述

It looks like there's a bug where alpha= is ignored, which you can workaround by modifying the artists after plotting (as above) or by defining the palette as [mpl.colors.to_rgba("C0", alpha=.5)] * Num .看起来有一个忽略alpha=的错误,您可以通过在绘图后修改艺术家(如上)或将调色板定义为[mpl.colors.to_rgba("C0", alpha=.5)] * Num

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

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