pandas 如何创建时钟极坐标图

imzjd6km  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(121)
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}}

你好,我想创建一个时钟图表,以显示在时钟表示的时间功耗.我们如何添加精确的时钟周围的极坐标数?和我们如何可以显示的权力,他们的时间在时钟?有人可以帮助我通过这个?提前感谢.
图像=
1

mzsu5hc0

mzsu5hc01#

正如我所评论的,我添加了一个类似24小时时钟的刻度标签,顶部设置了0:00,以参考@unutbu的答案。创建一个对应于24小时x 60分钟的Angular 的等距序列。将时间序列数据的小时和分钟转换为分钟。这个分数是之前创建的数字序列的索引。我已经更改了一些数据,以便更容易在图形中查看结果。

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()

相关问题