Can I color different months?

Question

My data has a datetime index. I've resampled the data and want to visualize it so that the different months all have a different color. Here is my data

Time        Count
2016-08-07  88
2016-08-14  95
2016-08-21  86
2016-08-28  81
2016-09-04  92
2016-09-11  89
2016-09-18  93
2016-09-25  83
2016-10-02  78
2016-10-09  90
2016-10-16  87
2016-10-23  79
2016-10-30  91
2016-11-06  90
2016-11-13  87
2016-11-20  97
2016-11-27  83
2016-12-04  63

How can I plot this data so that each month has a different color?


Show source
| pandas   | python   2017-01-07 22:01 2 Answers

Answers to Can I color different months? ( 2 )

  1. 2017-01-07 22:01

    This is not for a plot (I have added that below) but you can also color your dataframe text output if this is of interest. I find it can help me identify groups of data.

    I created a new df['month'] column and you can see the function I applied below to give you color for your months using style.

    df['month'] = df.index.to_datetime().month
    
    def color_monthd(val):
    
        color = 'blue' if val == 8\
        else 'orange' if val == 9\
        else 'purple' if val == 10\
        else 'red' if val == 11\
        else 'black'
    
        return 'color: %s' % color
    
    s = df.style.applymap(color_monthd)
    

    enter image description here

  2. 2017-01-07 23:01

    As per request for "each bar belonging to a seperate month to have all the same color". You can change the colours to whatever you like in the dict below and these are then used in the list comprehension.

    df['month'] = df.index.to_datetime().month
    
    colors = {8: 'r', 9: 'b', 10: 'g',11: 'y', 12: 'w',}
    df['Count'].plot(kind='bar', color=[colors[i] for i in df['month']])
    

    enter image description here

Leave a reply to - Can I color different months?

◀ Go back