【问题标题】:Float formatting: Plotting an strftime浮点格式:绘制 strftime
【发布时间】:2013-03-11 14:56:56
【问题描述】:

我正在格式化 MongoDB 文档中的原始时间戳。原来的样子是这样的:

"timestamp" : ISODate("2013-03-06T17:10:29Z")

还有格式化的(使用聚合后作为文档的_id):

"_id" : "06-03-13T17:10"

然后我想根据“_id”值(x 轴)绘制“数量”(y 轴)值

我在格式化 _id 以便绘制它时遇到问题。

amount = [book["price"] for book in sorted["result"]]
time = [book["_id"] for book in sorted["result"]]
P.plot(amount, time)
P.show()

返回:

ValueError: invalid literal for float(): 06-03-13T15:36

【问题讨论】:

    标签: python matplotlib strftime


    【解决方案1】:

    您正在向 plot 函数传递一个字符串,据我所知,这不是有效的 x 轴格式。我很确定您想要拥有原始的datetime 对象,然后使用date2num 格式化它们以传递给matplotlib。因此,根据您是否将strftime 格式化的东西用于其他任何事情,将date2num 的输出存储在您的映射中可能更有意义,而不是strftime 的输出。

    请参阅 this post 了解更多具体信息和一些示例的链接。

    【讨论】:

    • 谢谢,但我将如何设法删除我的日期时间对象中的秒数?
    • @Julia 您需要从日期时间中删除秒数,还是仅从格式字符串中删除?您应该能够为 matplotlib 提供一个 strftime 格式字符串以供使用。如果您需要从日期时间中删除秒数,那就是 dt = dt.replace(second=0)
    猜你喜欢
    • 2014-07-10
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2017-05-20
    • 2019-08-08
    相关资源
    最近更新 更多