Search code examples
python-3.xpandasmatplotlibpivot-tablelinegraph

Matplotlib Line Graph from Pivot Table: Adjust Color of Axis Tick Labels


Given the following:

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

How can I adjust the color of the y-axis tick labels?

I tried:

ax1.set_xticklabels(t.Count,color='grey')

...but no dice.

Also, if they were in 10s of thousands (i.e. 90000 for the top number), how can I get them to display as 90K or 90,000?

Thanks in advance!


Solution

  • I think you need:

    ax.tick_params(axis='x', color='grey', labelcolor='grey')
    

    EDIT:

    If you want display 90,000:

    from matplotlib.ticker import FuncFormatter
    
    ax.get_yaxis().set_major_formatter(FuncFormatter(lambda x, p: format(int(x), ',')))