如何着色大熊猫的消极和正面价值观'酒吧情节?

时间:2017-08-29 02:18:28

标签: python pandas

import numpy as np
import pandas as pd
from pandas import DataFrame, Series
from datetime import datetime
import seaborn as sns
import matplotlib.pyplot as plt
import mpld3

# Set some pandas options
pd.set_option('display.notebook_repr_html', False)
pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 10)

# seaborn settings
sns.set_palette("deep", desat=.6)
sns.set_context(rc={"figure.figsize": (8,4)})
# And some items for matplotlib
%matplotlib inline 

# plt.style.available
plt.style.use('seaborn')
# mpld3.enable_notebook()
gp = df.groupby(['year','department', 'worktype']).agg({'minutes':'sum'})
df1 = gp.loc[2016]
df2 = gp.loc[2017]
diff = (df2-df1).dropna()
diff['positive']=diff['minutes'] > 0
print(diff)
diff.plot(kind='bar', color=diff.positive.map({True: 'b', False: 'y'}))
plt.show()

我的差异数据集

                    minutes  positive
department worktype                   
TA         PHP           0.0     False
           PYTHON     -100.0     False
WEB        ANDROID       5.0      True
           PYTHON     -410.0     False

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将lambda函数应用于颜色参数。

import pandas as pd
%matplotlib inline

df=pd.DataFrame({'department':['TA','TA','WEB','WEB'],
                 'worktype':['PHP','PYTHON','ANDROID','PYTHON'],
                 'minutes':[0.0,-100.0,5.0,-410.0]})\
     .set_index(['department', 'worktype'])
df.plot(kind='bar',
        color=df.minutes.apply(lambda x: 'b' if x>0 else 'y'));