Search code examples
pythonpandasmatplotlibpolar-coordinates

How to create a clock polar chart


import numpy as np
import matplotlib.pyplot as plt


# Compute areas and colors

r= dt.iloc[0:9,2]
theta = dt.iloc[0:9,1]
area = r
colors = r

fig = plt.figure()[![enter image description here][1]][1]
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)





data= {'device': {0: 'Laptop', 1: 'Laptop', 2: 'Laptop', 3: 'Laptop', 4: 'Laptop'},
'power': {0: 20, 1: 23, 2: 20, 3: 22, 4: 19},
'time': {0: '16/11/2012 11:29',
 1: '16/11/2012 11:30',
 2: '16/11/2012 11:31',
 3: '16/11/2012 11:32',
 4: '16/11/2012 11:33'},
 'time_string': {0: '16/11/2012 11:29',
 1: '16/11/2012 11:30',
 2: '16/11/2012 11:31',
 3: '16/11/2012 11:32',
 4: '16/11/2012 11:33'},
 'x': {0: 0.17, 1: 0.17, 2: 0.17, 3: 0.17, 4: 0.17},
 'y': {0: 0.48, 1: 0.48, 2: 0.48, 3: 0.48, 4: 0.48}}

Hello, I want to create a clock chart to show power consumption by time in a clock representation. How can we add exact clock number around the polar coordinates? and How we can show the powers by their times in the clock? Could anyone please help me through this? Thanks in advance.

image=
1: https://i.sstatic.net/osXtG.png


Solution

  • As I commented, I added a scale label like a 24-hour clock, with 0:00 set at the top, in reference to @unutbu's answer. Create an isometric sequence corresponding to an angle of 24 hours x 60 minutes. Converts the hours and minutes of the time series data to minutes. This fraction is the index of the number sequence created earlier. I have changed some of your data to make it easier to see the results in the graph.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    data= {'device': {0: 'Laptop', 1: 'Laptop', 2: 'Laptop', 3: 'Laptop', 4: 'Laptop'},
    'power': {0: 60, 1: 23, 2: 120, 3: 22, 4: 49},
    'time': {0: '16/11/2012 11:29',
     1: '16/11/2012 12:30',
     2: '16/11/2012 13:31',
     3: '16/11/2012 14:32',
     4: '16/11/2012 15:33'},
     'time_string': {0: '16/11/2012 11:29',
     1: '16/11/2012 11:30',
     2: '16/11/2012 11:31',
     3: '16/11/2012 11:32',
     4: '16/11/2012 11:33'},
     'x': {0: 0.17, 1: 0.17, 2: 0.17, 3: 0.17, 4: 0.17},
     'y': {0: 0.48, 1: 0.48, 2: 0.48, 3: 0.48, 4: 0.48}}
    
    df = pd.DataFrame(data)
    df['time'] = pd.to_datetime(df['time'])
    df['seconds'] = df['time'].dt.hour*60+df['time'].dt.minute
    
    # Compute areas and colors
    hour_minute = np.linspace(0, 2*np.pi, 24*60, endpoint=False)
    
    r= df.iloc[0:9,1].tolist()
    theta = df.iloc[0:9,1]
    area = r
    colors = r
    
    fig = plt.figure()
    ax = fig.add_subplot(projection='polar')
    ax.scatter(hour_minute[df['seconds']], r, s=area, c=colors, cmap='hsv', alpha=0.75)
    ax.set_rgrids(np.arange(0,df['power'].max(),20), angle=90)
    
    # clock labels
    ax.set_xticks(np.linspace(0, 2*np.pi, 24, endpoint=False))
    ax.set_xticklabels(range(24))
    
    # make the labels go clockwise
    ax.set_theta_direction(-1)
    
    # place 0 at the top
    ax.set_theta_offset(np.pi/2.0) 
    
    plt.show()
    

    enter image description here