2016-05-17 46 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