【问题标题】:Pandas scatterplot categorical and timeseries axesPandas 散点图分类和时间序列轴
【发布时间】:2015-11-28 05:02:49
【问题描述】:

我希望创建一个类似于 nltk 的词汇分散图的图表,但我正在绘制一个空白如何构建它。我在想 scatter 将是我最好的geom,使用'|'作为标记,并设置alpha,但我在设置参数时遇到了各种问题。下面是一个例子:

我在 5 年期间使用日期时间索引 freq='D' 排列数据框,每列代表该日期使用的特定单词的计数。 例如:

tst = pd.DataFrame(index=pd.date_range(datetime.datetime(2010, 1, 1), end=datetime.datetime(2010, 2, 1), freq='D'), data=[[randint(0, 5), randint(0, 1), randint(0, 2)] for x in range(32)])

目前我正在尝试类似于以下内容:

plt.figure()
tst.plot(kind='scatter', x=tst.index, y=tst.columns, marker='|', color=sns.xkcd_rgb['dodger blue'], alpha=.05, legend=False)
yticks = plt.yticks()[0]
plt.yticks(yticks, top_words)

上面的代码产生一个 KeyError:

KeyError: "['2009-12-31T19:00:00.000000000-0500' '2010-01-01T19:00:00.000000000-0500'\n '2010-01-02T19:00:00.000000000-0500' '2010-01-03T19:00:00.000000000-0500'\n '2010-01-04T19:00:00.000000000-0500' '2010-01-05T19:00:00.000000000-0500'\n '2010-01-06T19:00:00.000000000-0500' '2010-01-07T19:00:00.000000000-0500'\n '2010-01-08T19:00:00.000000000-0500' '2010-01-09T19:00:00.000000000-0500'\n '2010-01-10T19:00:00.000000000-0500' '2010-01-11T19:00:00.000000000-0500'\n '2010-01-12T19:00:00.000000000-0500' '2010-01-13T19:00:00.000000000-0500'\n '2010-01-14T19:00:00.000000000-0500' '2010-01-15T19:00:00.000000000-0500'\n '2010-01-16T19:00:00.000000000-0500' '2010-01-17T19:00:00.000000000-0500'\n '2010-01-18T19:00:00.000000000-0500' '2010-01-19T19:00:00.000000000-0500'\n '2010-01-20T19:00:00.000000000-0500' '2010-01-21T19:00:00.000000000-0500'\n '2010-01-22T19:00:00.000000000-0500' '2010-01-23T19:00:00.000000000-0500'\n '2010-01-24T19:00:00.000000000-0500' '2010-01-25T19:00:00.000000000-0500'\n '2010-01-26T19:00:00.000000000-0500' '2010-01-27T19:00:00.000000000-0500'\n '2010-01-28T19:00:00.000000000-0500' '2010-01-29T19:00:00.000000000-0500'\n '2010-01-30T19:00:00.000000000-0500' '2010-01-31T19:00:00.000000000-0500'] not in index" 

任何帮助将不胜感激。

在帮助下,我能够生成以下内容:

plt.plot(tst.index, tst, marker='|', color=sns.xkcd_rgb['dodger blue'], alpha=.25, ms=.5, lw=.5)
plt.ylim([-1, 20])
plt.yticks(range(20), top_words)

不幸的是,只有当有相应的栏要在其上构建时,才会显示上面的栏。我的数据不是这样的。

【问题讨论】:

    标签: pandas matplotlib scatter-plot categorical-data timeserieschart


    【解决方案1】:

    我不确定你是否可以使用.plot 方法来做到这一点。不过,直接在matplotlib里做就很容易了:

    plt.plot(tst.index, tst, marker='|', lw=0, ms=10)
    plt.ylim([-0.5, 5.5])
    

    【讨论】:

    • 工作几乎完全符合预期。不过,我的轴心确实有一点转变。我对 0 的论点在底部形成一个小条,每隔一个整数形成一条向上的线。我会在我的问题中发布结果。
    【解决方案2】:

    如果可以安装seaborn,试试stripplot():

    import seaborn as sns
    sns.stripplot(data=tst, orient='h', marker='|', edgecolor='blue');
    

    请注意,我更改了您的数据以使其看起来更有趣:

    tst = pd.DataFrame(index=pd.date_range(datetime.datetime(2010, 1, 1), end=datetime.datetime(2010, 2, 1), freq='D'), 
                       data=(150000 * np.random.rand(32, 3)).astype('int'))
    

    更多关于seaborn的信息:

    http://stanford.edu/~mwaskom/software/seaborn/tutorial/categorical.html

    【讨论】:

    • 是的,这很好用。我在文档中遇到过这个模块,但以前无法访问它。我使用的是过时的 seaborn 版本。感谢您的建议!
    • 不过,我确实想说,底部的刻度应该读取日期。从我的原始数据集中,散点应该在列和索引的交点处,根据数据中的度数变暗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2015-05-19
    • 2021-02-22
    • 2021-05-08
    相关资源
    最近更新 更多