英文:
color parameter isn't changing the plot color
问题
以下是代码部分的翻译:
import numpy as np
import pandas as pd
import seaborn as sns
sns.displot(pd.DataFrame(res), color='red', edgecolor=None, binwidth=.5, binrange=(3, 18+1));
res 是一个列表类型。
我原本期望柱状图是红色的,但它们显示为默认颜色。
我之前使用这段代码时没有问题。
英文:
Following is the code that doesn't work.
import numpy as np
import pandas as pd
import seaborn as sns
sns.displot(pd.DataFrame(res), color='red', edgecolor=None, binwidth=.5, binrange=(3, 18+1));
res is a list type.
I was expecting the bars to be red, but they are the default color.
I had no problem before with this code.
答案1
得分: 2
- 
设置单个分组的颜色的正确方法取决于如何或是否指定了
data=,以及是否指定了x=或y=。 - 
对于
g = sns.histplot(data=df, color='r', legend=False),同样的问题也适用,如此图例所示。- 同样的实现也适用于 
histplot。 
 - 同样的实现也适用于 
 - 
在
python 3.11.3、matplotlib 3.7.1、seaborn 0.12.2中进行了测试 
导入和数据
import numpy as np
import seaborn as sns
# 将随机正态数据存储为列表
np.random.seed(2023)
res = np.random.normal(loc=75, scale=5, size=1000).tolist()
# 创建一个数据框
df = pd.DataFrame(res)
# df.head() - 列标题是数字 0,而不是字符串 '0'
           0
0  78.558368
1  73.377575
2  69.990647
3  76.181254
4  74.489201
绘图实现
- 传递 
df,不指定x=的列名,需要传递一个dict或一个list给palette。palette也可以接受一个字符串,但字符串必须是有效的调色板名称,ValueError: 'r' 不是有效的调色板名称
 
g = sns.displot(data=df, palette={0: 'r'})
g = sns.displot(data=df, palette=['r'])
- 传递 
df,并为x=指定列名 
g = sns.displot(data=df, x=0, color='r')
- 将列传递给 
x= 
g = sns.displot(x=df[0], color='r')
- 直接将 
res传递给data= 
g = sns.displot(data=res, color='r')
- 直接将 
res传递给x= 
g = sns.displot(x=res, color='r')
英文:
- The correct approach to setting the color of a single group, depends on how, or if, 
data=is specified, and ifx=, ory=, is specified. - The same issue applies to 
g = sns.histplot(data=df, color='r', legend=False), as shown in this plot.- The same implementations work with 
histplot. 
 - The same implementations work with 
 - Tested in 
python 3.11.3,matplotlib 3.7.1,seaborn 0.12.2 
Imports and Data
import numpy as np
import seaborn as sns
# random normal data as a list
np.random.seed(2023)
res = np.random.normal(loc=75, scale=5, size=1000).tolist()
# create a dataframe
df = pd.DataFrame(res)
# df.head() - the column header is the number 0, not string '0'
           0
0  78.558368
1  73.377575
2  69.990647
3  76.181254
4  74.489201
Plot Implementation
- Pass 
df, and do not specify a column name forx=, requires passing adictor alisttopalette.palettewill also accept a string, but the string must be a valid palette name,ValueError: 'r' is not a valid palette name
 - The color saturation is different when using 
palette, compared tocolor, so passalpha=(number 0 - 1) to adjust as needed. 
g = sns.displot(data=df, palette={0: 'r'})
g = sns.displot(data=df, palette=['r'])
- Pass 
df, and specify a column name forx= 
g = sns.displot(data=df, x=0, color='r')
- Pass the column to 
x= 
g = sns.displot(x=df[0], color='r')
- Pass 
resdirectly todata= 
g = sns.displot(data=res, color='r')
- Pass 
resdirectly tox= 
g = sns.displot(x=res, color='r')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论