3

我期待创建一个很像nltk的词法分散图的图表,但是我正在绘制一个空白的如何构建这个图表。我在想分散是我最好的几何,用'|'作为标记,并设置alpha,但我遇到了各种设置参数的问题。这样的一个例子是下面:熊猫散点图分类和时间轴

enter image description here

我有布置成与日期时间指数,频率=“d”的数据帧,在5年的时间,并且每列表示使用的特定词的计数日期。 例如:

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) 

enter image description here

不幸的是,它只是似乎是上条将显示当存在要在上面建了相应的条的。这不是我数据的外观。

回答

1

我不确定你可以用.plot方法做到这一点。然而,很容易笔直地做在matplotlib

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

enter image description here

+0

预期一样几乎一模一样。尽管如此,我的确有一些转变。我的0的参数在底部形成一个小条,其中每一个整数形成一条直线。我会在我的问题中发布结果。 – hyleaus

1

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

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

plot

注意,我改变你的数据,使其看起来更有趣:

在seaborn
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')) 

的更多信息:

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

+0

是的,这工作得很好。我在文档中遇到过这个模块,但之前无法访问它。我使用的是过时的seaborn版本。感谢您的建议! – hyleaus

+0

不过,我想说的是,底部的比例应该是阅读日期。从我的原始数据集中,散点应该位于列和索引的交点处,并且根据数据的程度使点变暗。 – hyleaus