以字符串为轴的时间序列图

time series plot with strings in axis

我有一个小的时间序列

                    created    current_page
0  2020-02-29T19:26:00.000Z        SOMMARIO
1  2020-02-29T19:25:00.000Z  DATI PERSONALI
2  2020-02-29T19:25:00.000Z    OFFERTA FULL
3  2020-02-29T19:24:00.000Z       DATI BENE
4  2020-02-29T19:23:00.000Z        HOMEPAGE

我如何绘制它并在 x 轴上获取 "created" 列中的日期,在 y 轴上获取 "current page" 元素? 到目前为止,我已经尝试将标签编码器用于 y 轴值,但是当我这样做时,x 轴上也会出现浮点数而不是日期。我的代码是

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
series["current_page"] = le.fit_transform(series["current_page"].values)
series.plot()
plt.gcf().autofmt_xdate()

然后我得到

试试这个,我已经在我的本地机器上检查过它有效:

# imports
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# data
created = ["2020-02-29T19:26:00.000Z", 
           "2020-02-29T19:25:00.000Z", 
           "2020-02-29T19:25:00.000Z", 
           "2020-02-29T19:24:00.000Z",
           "2020-02-29T19:23:00.000Z"]

current_page = ["SOMMARIO", "DATI PERSONALI", "OFFERTA FULL", "DATI BENE", "HOMEPAGE"]

# create df from data
df = pd.DataFrame([created, current_page], ["created", "current_page"]).T

# convert to datetime
df["created"] = pd.to_datetime(df["created"])

# enconde data
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df["current_page"] = le.fit_transform(df["current_page"].values)

# separte x and y
x = df["created"]
y = df["current_page"]

# plot data
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)