2016-05-17 43 views
0

的自定义色彩鉴于以下几点:在t.plotMatplotlib线图从数据透视表:行

import pandas as pd 
import numpy as np 
%matplotlib inline 
df = pd.DataFrame(
     {'YYYYMM':[201603,201503,201403,201303,201603,201503,201403,201303], 
     'Count':[5,6,2,7,4,7,8,9], 
     'Group':['A','A','A','A','B','B','B','B']}) 
df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2] 
t=df.pivot_table(df,index=['YYYYMM'],columns=['Group'],aggfunc=np.sum) 

fig, ax = plt.subplots(1,1) 
t.plot(ax=ax) 

是否有参数(),让我可以指定每行的颜色?

enter image description here

提前感谢!

回答

1

您可以提供的线条样式:

t.plot(ax=ax, style=['yellow', 'red']) 

enter image description here

+1

超级想法,你改变答案的颜色的全名。 ;)似乎更好的解决方案。 – jezrael

+0

是的,单字母版本仅适用于快速测试。 –

1

您可以使用:

ax.set_color_cycle(['red', 'black']) 

graph

样品:

df = pd.DataFrame(
     {'YYYYMM':[201603,201503,201403,201303,201603,201503,201403,201303], 
     'Count':[5,6,2,7,4,7,8,9], 
     'Group':['A','A','A','A','B','B','B','B']}) 
df['YYYYMM']=df['YYYYMM'].astype(str).str[:-2] 
t=df.pivot_table(df,index=['YYYYMM'],columns=['Group'],aggfunc=np.sum) 

fig, ax = plt.subplots(1,1) 
ax.set_color_cycle(['red', 'black']) 
t.plot(ax=ax) 

编辑:

非常有趣,通过它似乎更是用颜色的全名,因为它与Mike 1. answer

t.plot(ax=ax, style=['yellow', 'red']) 
+0

嗨Jezrael,它扔了弃用警告使用set_prop_cycle,但无论哪种方式,它似乎并没有为我工作。如果这有所作为,我正在使用Python 3.5。 –

+0

有趣的是,我也使用python 3.5。 – jezrael