【问题标题】:Plotting time series with seaborn用 seaborn 绘制时间序列
【发布时间】:2015-05-19 06:15:20
【问题描述】:

我有 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 轴,而不是生成的值。

【问题讨论】:

  • 您可能希望看到我对提供示例数据的评论here
  • 我添加了一部分实际数据。时间已经排序了。
  • 如果您提供一个小而完整的问题示例,您更有可能获得帮助——数据和失败的代码行,我们可以一次操作将其剪切并粘贴到我们的环境中一起工作。

标签: python matplotlib seaborn


【解决方案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()

【讨论】:

  • 你也可以在 seaborn 被导入时只使用 matplotlib 函数绘图,它会采用 seaborn 风格。
猜你喜欢
  • 1970-01-01
  • 2014-10-31
  • 2014-05-12
  • 2016-11-28
  • 2020-06-06
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 2018-03-29
相关资源
最近更新 更多