Seaborn 막대 그래프에서 막대 색상을 설정하는 방법


다음 방법을 사용하여 해양 막대 그래프의 막대 색상을 설정할 수 있습니다.

방법 1: 모든 막대의 색상 설정

 #use steelblue for the color of all bars
sns. barplot (x=xvar, y=yvar, color=' steelblue ')

방법 2: 막대 색상을 최대값으로 설정

 #use orange for bar with max value and gray for all other bars
cols = [' gray ' if (x < max (df. yvar )) else ' orange ' for x in df. yvar ]

#create barplot using specified colors
sns. barplot (x=df. xvar , y=df. yvar , palette=cols)

방법 3: 상태에 따라 막대 색상 설정

 #use red for bars with value less than 10 and green for all other bars
cols = [' red ' if x < 10 else ' green ' for x in df. yvar ]

#create barplot using specified colors
sns. barplot (x=df. xvar , y=df. yvar , palette=cols)

다음 예에서는 다음 pandas DataFrame에서 실제로 각 메서드를 사용하는 방법을 보여줍니다.

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' employee ': ['Andy', 'Bert', 'Chad', 'Doug', 'Eric', 'Frank'],
                   ' sales ': [22, 14, 9, 7, 29, 20]})

#view DataFrame
print (df)

  employee sales
0 Andy 22
1 Bert 14
2 Chad 9
3 Doug 7
4 Eric 29
5 Frank 20

예 1: 모든 막대의 색상 설정

다음 코드는 Seaborn에서 막대 그래프를 생성하고 플롯의 모든 막대에 “steelblue” 색상을 사용하는 방법을 보여줍니다.

 import seaborn as sns

#create barplot using steelblue as color for each bar
sns. barplot (x= df.employee ,y= df.sales ,color=' steelblue ')

예시 2: 최대값으로 막대 색상 설정

다음 코드는 막대 그래프에서 최대값이 있는 막대에 주황색을 사용하고 다른 모든 막대에는 회색을 사용하는 방법을 보여줍니다.

 import seaborn as sns

#use orange for bar with max value and gray for all other bars
cols = [' gray ' if (x < max (df. sales )) else ' orange ' for x in df. dirty ]

#create barplot with custom colors
sns. barplot (x=df. employee , y=df. sales , palette=cols) 

seaborn barplot은 최대 값으로 막대 색상을 설정합니다.

예시 3: 최대값으로 막대 색상 설정

다음 코드는 막대 그래프에서 최대값이 있는 막대에 주황색을 사용하고 다른 모든 막대에는 회색을 사용하는 방법을 보여줍니다.

 import seaborn as sns

#use red for bars with value less than 10 and green for all other bars
cols = [' red ' if x < 10 else ' green ' for x in df. dirty ]

#create barplot with custom colors
sns. barplot (x=df. employee , y=df. sales , palette=cols) 

조건에 따라 색상이 표시된 Seaborn 막대 그래프

추가 리소스

다음 튜토리얼에서는 seaborn에서 다른 일반적인 기능을 수행하는 방법을 설명합니다.

Seaborn에서 그룹화된 막대 그래프를 만드는 방법
Seaborn에서 원형 차트를 만드는 방법
단일 그림에 여러 Seaborn 플롯을 생성하는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다