数据透视表中的Matplotlib线图:线的自定义颜色

时间:2016-05-17 16:19:17

标签: python-3.x pandas matplotlib pivot-table linegraph

鉴于以下内容:

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)

t.plot()中是否有参数允许我指定每一行的颜色?

enter image description here

提前致谢!

2 个答案:

答案 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'])

答案 1 :(得分:1)

您可以提供线型:

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

enter image description here