Matplotlib时间表

Agr*_*ag9 8 python timeline matplotlib pandas

我正在寻找带有一堆时间轴的python DataFrame并将它们绘制在一个图中.DataFrame索引是Timestamps,并且有一个特定的列,我们称之为"sequence",它包含"A"和"B"之类的字符串.所以DataFrame看起来像这样:

+--------------------------+---+
| 2014-07-01 00:01:00.0000 | A |
+--------------------------+---+
| 2014-07-01 00:02:00.0000 | B |
+--------------------------+---+
| 2014-07-01 00:04:00.0000 | A |
+--------------------------+---+
| 2014-07-01 00:08:00.0000 | A |
+--------------------------+---+
| 2014-07-01 00:08:00.0000 | B |
+--------------------------+---+
| 2014-07-01 00:10:00.0000 | B |
+--------------------------+---+
| 2014-07-01 00:11:00.0000 | B |
+--------------------------+---+
Run Code Online (Sandbox Code Playgroud)

我正在寻找这样的情节:

B |  *     * **
A | *  *   *
  +------------
    Timestamp
Run Code Online (Sandbox Code Playgroud)

Pau*_*l H 12

我只想使用字典将每个类别映射到y值.

import random
import numpy as np
import matplotlib.pyplot as plt
import pandas

categories = list('ABCD')

# map categories to y-values
cat_dict = dict(zip(categories, range(1, len(categories)+1)))

# map y-values to categories
val_dict = dict(zip(range(1, len(categories)+1), categories))

# setup the dataframe
dates = pandas.DatetimeIndex(freq='20T', start='2012-05-05 13:00', end='2012-05-05 18:59')
values = [random.choice(categories) for _ in range(len(dates))]
df = pandas.DataFrame(data=values, index=dates, columns=['category'])

# determing the y-values from categories
df['plotval'] = df['category'].apply(cat_dict.get)

# make the plot
fig, ax = plt.subplots()
df['plotval'].plot(ax=ax, style='ks')
ax.margins(0.2)

# format y-ticks look up the categories
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, pos: val_dict.get(x)))
Run Code Online (Sandbox Code Playgroud)

我得到:

在此输入图像描述

请注意,由于您可能已经拥有数据框,因此可以构建cat_dict并使用val_dict以下内容:

# map categories to y-values
cat_dict = dict(zip(pandas.unique(df['category']), range(1, len(categories)+1)))

# map y-values to categories
val_dict = dict(zip(range(1, len(categories)+1), pandas.unique(df['category'])))
Run Code Online (Sandbox Code Playgroud)