使用matplotlib创建每周时间表

时间:2014-01-02 21:39:20

标签: python numpy matplotlib

修改:我将数据类型更改为Pandas DataFrame,看起来像(datetime.datetime,int),以使问题更简单。

原帖:

我有一大堆数据报告,看起来像(datetime.datetime,int,int),我似乎无法正确绘制。我需要X轴是24小时和这个阵列

np.array([datetime.datetime.time(x) for x in DataArr])

Y应该是日期时间(星期一,星期二等)的日子 并且int应该为不同的事件给我不同的颜色,但我找不到一个例子 在matplotlib的网站上。

我正在寻找的一个例子:

enter image description here

1 个答案:

答案 0 :(得分:1)

听起来你想要这样的东西?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# I'm using pandas here just to easily create a series of dates.
time = pd.date_range('01/01/2013', '05/20/2013', freq='2H')
z = np.random.random(time.size)

# There are other ways to do this, but we'll exploit how matplotlib internally
# handles dates. They're floats where a difference of 1.0 corresponds to 1 day.
# Therefore, modulo 1 results in the time of day. The +1000 yields a valid date.
t = mdates.date2num(time) % 1 + 1000

# Pandas makes getting the day of the week trivial...
day = time.dayofweek

fig, ax = plt.subplots()
scat = ax.scatter(t, day, c=z, s=100, edgecolor='none')
ax.xaxis_date()

ax.set(yticks=range(7),
       yticklabels=['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'])

# Optional formatting tweaks
ax.xaxis.set_major_formatter(mdates.DateFormatter('%l%p'))
ax.margins(0.05)
fig.colorbar(scat)
plt.show()

enter image description here