【问题标题】:plotting multiple date-time columns on y-axis and their corresponding ids on x-axis在 y 轴上绘制多个日期时间列,在 x 轴上绘制它们对应的 id
【发布时间】:2020-01-24 11:00:17
【问题描述】:

我有一个 csv/excel 文件,其中包含文档 ID 及其相应的日期时间列。尝试了以下方法:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime
import pandas as pd
xls_data = pd.read_excel(<file-path>)

x1 = xls_data["client_doc_id"].values #id column in the file
y1 = xls_data["planned_finish_ifd"].values #one of the date-time column in the file

plt.plot(x1,y1)
plt.gcf().autofmt_xdate()
plt.show()

但我无法获得所需的输出,而是显示以下错误:

AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

如何绘制一个日期时间列以及如何在一张图中绘制多个日期时间列?

【问题讨论】:

  • 尝试使用x1 = pd.to_datetime(xls_data["client_doc_id"]) 而不是使用values 属性
  • 我假设您想说y1 = pd.to_datetime(xls_data["planned_finish_ifd"]) 而不是x1 = pd.to_datetime(xls_data["client_doc_id"])。因此我以这种方式使用但我得到了另一个错误:ValueError: could not convert string to float: '200-20-PE-DEC-0001'
  • 如果不是,我不明白为什么必须将文档 ID 字段更改为日期时间?因为x1 = pd.to_datetime(xls_data["client_doc_id"]) 与文档id 列相关。
  • 对不起,我的意思是尝试 y1 = pd.to_datetime(xls_data["planned_finish_ifd"]) 而不是 y1 = xls_data["planned_finish_ifd"],但在制定和输入我的评论之间的某个地方,我的评论被切换到了 x 值。
  • 尝试过但又遇到了另一个错误:ValueError: could not convert string to float: '200-20-PE-DEC-0001'。其中'200-20-PE-DEC-0001' 是文档ID。

标签: python python-3.x date matplotlib data-visualization


【解决方案1】:
x1 = xls_data["client_doc_id"]
y1 = xls_data["planned_finish_ifd"]

而不是.values,通常将其用作系列。

因为我的matplotlib版本是2.0.2,一开始我没有得到。然后我将其更新到最新版本。现在它起作用了。附上情节的图像。 (忽略包含文档 ID 的 x 轴)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多