Matplotlib and Numpy - Create a calendar heatmap

后端 未结 4 813
旧时难觅i
旧时难觅i 2020-12-24 04:09

Is it possible to create a calendar heatmap without using pandas? If so, can someone post a simple example?

I have dates like Aug-16 and a count value like 16 and I

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 04:44

    Below is a code that can be used to generate a calendar map for daily profiles of a value.

    """
    Created on Tue Sep  4 11:17:25 2018
    
    @author: woldekidank
    """
    
    import numpy as np
    from datetime import date
    import datetime
    import matplotlib.pyplot as plt
    import random
    
    
    D = date(2016,1,1)
    Dord = date.toordinal(D)
    Dweekday = date.weekday(D)
    
    Dsnday = Dord - Dweekday + 1 #find sunday
    square = np.array([[0, 0],[ 0, 1], [1, 1], [1, 0], [0, 0]])#x and y to draw a square
    row = 1
    count = 0
    while row != 0:
        for column in range(1,7+1):    #one week per row
            prof = np.ones([24, 1])
            hourly = np.zeros([24, 1])
            for i in range(1,24+1):
                prof[i-1, 0] = prof[i-1, 0] * random.uniform(0, 1)
                hourly[i-1, 0] = i / 24
            plt.title('Temperature Profile')
            plt.plot(square[:, 0] + column - 1, square[:, 1] - row + 1,color='r')    #go right each column, go down each row
            if date.fromordinal(Dsnday).month == D.month:
                if count == 0:
                    plt.plot(hourly, prof)
                else:
                    plt.plot(hourly + min(square[:, 0] + column - 1), prof + min(square[:, 1] - row + 1))
    
                plt.text(column - 0.5, 1.8 - row, datetime.datetime.strptime(str(date.fromordinal(Dsnday)),'%Y-%m-%d').strftime('%a'))
                plt.text(column - 0.5, 1.5 - row, date.fromordinal(Dsnday).day)
    
            Dsnday = Dsnday + 1
            count = count + 1
    
        if date.fromordinal(Dsnday).month == D.month:
            row = row + 1    #new row
        else:
            row = 0    #stop the while loop
    

    Below is the output from this code

    Image of data series on calendar days

提交回复
热议问题