来自 Pivot Table 的 Matplotlib 折线图:线条的自定义颜色

Matplotlib Line Graph from Pivot Table: Custom Color of Lines

鉴于以下情况:

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() 中是否有参数允许我指定每行的颜色?

提前致谢!

您可以使用:

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

样本:

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

您可以提供线条样式:

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