2017-11-08 5 views
1

다음 막대 그림을 고려하십시오. enter image description here목록 작성을 사용하여 색상을 어떻게 설정할 수 있습니까?

파란색으로 색 40000보다 높은 막대가하기 위해

빨간색으로 색 40000보다 낮은 바, 나는 다음과 같은 지능형리스트를 사용하려고 :

df.count().plot(kind = 'bar', color = ['powderblue' if df[e].count() > 40000 else 'red' for e in df]) 
  • df 내 dataframe
  • 입니다
  • df[e].count()은 각 열에 포함 된 NaN 이외의 값의 수를 반환해야합니다.

그러나 matplotlib는 대신 다음을 반환합니다. enter image description here

... 모두 빨간색입니다.

그건 모두 따로 촬영 때 똑같은리스트 통합이 완벽하게 잘 작동하는지 더 이상 :

colors = ['powderblue' if df[e].count() > 40000 else 'red' for e in df] 
Print(colors) 

powderblue 
powderblue 
powderblue 
powderblue 
powderblue 
powderblue 
powderblue 
powderblue 
red 
... 

사람이 내가 여기에 누락 무엇인지 설명해 주시겠습니까?

EDIT : 데이터 프레임은 다음과 같다 :

code url creator quantity brands 
1  3  NaN  B  0.5  Ta 
2  NaN Se  A  3.8  De 
3  6  Th  D  6.8  NaN 
4  2  Fr  C  NaN  Be 
5  1  Il  F  2.4  Pm 
... 

MCVE을 :

# creating an array of shape 10x10 
array = np.random.choice(10, size = (10, 10)) 

# transforming it in a dataframe and replacing zeros and ones by NaN 
df = pd.DataFrame(array).replace((0,1), np.nan) 
print(df) 

    0 1 2 3 4 5 6 7 8 9 
0 8.0  NaN  6 5 8.0  NaN  NaN  2.0  NaN  7.0 
1 7.0  8.0  7 8 9.0  8.0  9.0  8.0  8.0  5.0 
2 9.0  8.0  7 7 6.0  8.0  8.0  7.0  8.0  6.0 
3 4.0  4.0  3 3 3.0  5.0  4.0  2.0  6.0  4.0 
4 5.0  7.0  4 9 2.0  8.0  NaN  7.0  NaN  5.0 
5 7.0  6.0  6 7 NaN  5.0  NaN  5.0  4.0  3.0 
6 6.0  8.0  5 5 4.0  NaN  3.0  NaN  9.0  2.0 
7 9.0  5.0  4 3 NaN  7.0  6.0  4.0  8.0  NaN 
8 NaN  2.0  8 8 7.0  7.0  2.0  9.0  3.0  5.0 
9 3.0  9.0  6 3 9.0  NaN  9.0  7.0  2.0  8.0 

# creating a working list comprehension 
colors = ['blue' if df[e].count() > 8 else 'red' for e in df] 
print(colors) 

'blue', 'blue', 'blue', 'blue', 'red', 'red', 'red', 'blue', 'red', 'blue' 

# plotting the dataframe using the same list comprehension 
df.count().plot(kind = 'bar', color = colors) 

enter image description here

+1

당신은 당신의 데이터 프레임이 어떻게 생겼는지의 예를 들어 주실 수 있습니까? – James

+0

예, 방금 제 질문을 편집했습니다 – solub

+0

[mcve]를 찾으실 수 있습니까? df.count(). plot (kind = 'bar', color = [ 'df [e] .count()가 4이면'powderblue ']를 사용하여 붙여 넣은 데이터 프레임으로 코드를 시도했습니다. 40000 대신 df])'를 사용하고 예상대로 4 개의 빨간색과 하나의 파란색 '작성자'를 얻습니다. – DSM

답변

0

I 사용 된하기 matplotlib 버전 (2.0.2)가 오래된 여하튼 막았다 목록 이해력이 올바르게 실행됩니다. 버전 2.1.0에서이 문제가 해결되었습니다.

DavidG에게 제안을 주신 데 감사드립니다.

the list comprehension works perfectly fine with the lastest version of matplotlib (2.1.0)