用seaborn绘制时间序列

时间:2015-03-16 23:10:56

标签: python matplotlib seaborn

我有2个清单。一个代表时间,看起来像

time=[datetime.datetime(2015, 1, 2, 0, 1, 2),datetime.datetime(2015, 1, 2, 0, 5, 2),datetime.datetime(2015, 1, 3, 0, 1, 53),datetime.datetime(2015, 1, 3, 0, 1, 56),datetime.datetime(2015, 1, 5, 0, 1, 2),datetime.datetime(2015, 1, 5, 0, 1, 40),datetime.datetime(2015, 1, 7, 0, 1, 2),datetime.datetime(2015, 1, 7, 0, 1, 30),datetime.datetime(2015, 1, 9, 0, 1, 2),datetime.datetime(2015, 1, 9, 0, 1, 20)]

,另一个代表相应的数据点:

data=[51.024,3.2179,105.18,31.176,1.1123,1.7861,109.65,0.0,123.890,523.897]

用matplotlib绘制它很容易,但其余的统计数据都是用seaborn完成的,我想保留视觉效果并使用seaborn来获得整个结果集。当我使用seaborn.tsplot时,我收到以下错误index contains duplicate entries, cannot reshape seaborn。数据列表确实包含重复项,但它们位于不同的时间点,无法删除。我做错了什么?

编辑:如果我创建pandas数据帧,我可以使用sns.tsplot(y)绘制y值,但我希望能够将我的值用于x轴,而不是生成的值。

1 个答案:

答案 0 :(得分:1)

作为使用seaborn绘制数据的替代方法,您可以使用matplotlib的样式功能获得相同的外观,同时仍在matplotlib内绘图:

from matplotlib import style
# Seaborn's visual styling was inspired by ggplot,
#   so this style should be very similar:
style.use('ggplot')

import matplotlib.pyplot as plt
import datetime
time=[datetime.datetime(2015, 1, 2, 0, 1, 2),datetime.datetime(2015, 1, 2, 0, 5, 2),datetime.datetime(2015, 1, 3, 0, 1, 53),datetime.datetime(2015, 1, 3, 0, 1, 56),datetime.datetime(2015, 1, 5, 0, 1, 2),datetime.datetime(2015, 1, 5, 0, 1, 40),datetime.datetime(2015, 1, 7, 0, 1, 2),datetime.datetime(2015, 1, 7, 0, 1, 30),datetime.datetime(2015, 1, 9, 0, 1, 2),datetime.datetime(2015, 1, 9, 0, 1, 20)]
data=[51.024,3.2179,105.18,31.176,1.1123,1.7861,109.65,0.0,123.890,523.897]

# Replace this with whatever code you were using to plot
#   within matplotib, the styling should still be applied
plt.plot(time, data, 'ro')
plt.show()