用 seaborn 绘制时间序列
Plotting time series with 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 轴,而不是生成的值。
作为使用 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()
我有 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 轴,而不是生成的值。
作为使用 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()