2016-12-04 3 views
2

도표의 모든 조각이 프로그램 전체에서 일정하게 유지되는 누적 막 대형 차트를 만들려고하지만 df.plot을 얻는 방법을 알 수 없습니다 사용자 정의 팔레트를 사용합니다.스택 막대 차트 (Python)에서 사용자 정의 색상 표 사용

나는이 프로그램에서 20 가지 다른 보고서를 작성하면 프리즈가 항상 예를 ​​들어 파란색으로 표시되고 우박은 항상 흰색으로 표시되도록하고 싶습니다.

아래 예를 살펴 보시기 바랍니다. 먼저 모든 날씨 값에 대한 사용자 팔레트를 만듭니다.

나는 "쌍"팔레트를 사용하여 플로팅 작업을 수행 할 때, 그것은 잘 작동하고 다음과 같이 보이는

:

enter image description here

그리고 이것은 사용자 정의 팔레트

를 사용하여 제외하고 내가 원하는 기본적으로

01 : 그러나, 나는

df.plot(kind='bar', stacked=True,colormap=pal_weather) 

을 할 때 나는으로 끝나는 오류 23,516,

C:\Anaconda3\lib\site-packages\pandas\tools\plotting.py in _get_standard_colors(num_colors, colormap, color_type, color) 
157    if colormap is None: 
158     raise ValueError("Colormap {0} is not recognized".format(cmap)) 
--> 159   colors = lmap(colormap, np.linspace(0, 1, num=num_colors)) 
    160  elif color is not None: 
    161   if colormap is not None: 

C:\Anaconda3\lib\site-packages\pandas\compat\__init__.py in lmap(*args, **kwargs) 
    116 
    117  def lmap(*args, **kwargs): 
--> 118   return list(map(*args, **kwargs)) 
    119 
    120  def lfilter(*args, **kwargs): 

TypeError: 'dict' object is not callable 

이 어떤 도움이 많이 apopreciated 될 것이다

감사합니다!

샘플 코드는 다음과 같습니다

weather=('Day','Freeze', 'Wind', 'Flood', 'Quake', 'Hail') 

pal_weather = dict(zip(weather, sns.color_palette("cubehelix", n_colors=len(weather)))) 

data1 = [[ "M", 66386, 174296, 75131, 577908, 32015], 
    [ "T", 58230, 381139, 78045, 99308, 160454], 
    [ "W", 89135, 80552, 152558, 497981, 603535], 
    [ "T", 78415, 81858, 150656, 193263, 69638], 
    [ "F", 139361, 331509, 343164, 781380, 52269]] 


df = DataFrame(data=data1) 
df.columns = ('Day','Freeze', 'Wind', 'Flood', 'Quake', 'Hail') 
df.plot(kind='bar', stacked=True,colormap='Paired') 

답변

1

내 솔루션을보십시오 :

import matplotlib.pyplot as plt 
import matplotlib 
import seaborn as sns 
from matplotlib.colors import LinearSegmentedColormap 

matplotlib.style.use('ggplot') 
from pandas import DataFrame 

weather = ('Day', 'Freeze', 'Wind', 'Flood', 'Quake', 'Hail') 
colors = sns.color_palette("cubehelix", n_colors=len(weather)) 
cmap1 = LinearSegmentedColormap.from_list("my_colormap", colors) 

data1 = [["M", 66386, 174296, 75131, 577908, 32015], 
     ["T", 58230, 381139, 78045, 99308, 160454], 
     ["W", 89135, 80552, 152558, 497981, 603535], 
     ["T", 78415, 81858, 150656, 193263, 69638], 
     ["F", 139361, 331509, 343164, 781380, 52269]] 


df = DataFrame(data=data1) 
df.columns = weather 
df = df.set_index('Day') 
df.plot(kind='bar', stacked=True, colormap=cmap1) 
plt.show() 

출력 :

이 작동하는 것 같다

enter image description here

+0

하지만, 약간의 혼란과 같은 방법으로 색깔은 실제로 날씨 값과 관련이 있습니다. 설명해 주시겠습니까? 정말 고맙겠습니다. –

+0

@StumblingThroughDataScience 작동하는 경우, 대답을 올바른 것으로 표시하십시오. – eyllanesc

+0

시장을 참조하십시오 ... 이유에 대해 조금 혼란 스럽습니다. 도움을 많이 주시면 고맙겠습니다. –

관련 문제