【发布时间】:2021-06-02 14:17:41
【问题描述】:
使用的数据:(使用 CSV 库提取)
handler,first_test,handler_arrived
hvvm-02,7/15/2020,1/26/2020
hvvm-03,4/8/2020,1/26/2020
hvvm-04,5/27/2020,1/26/2020
hvvm-07,7/8/2020,1/26/2020
hvvm-09,2/26/2020,1/7/2020
hvvm-10,2/26/2020,1/7/2020
hvvm-11,7/1/2020,1/6/2020
hvvm-12,7/15/2020,1/6/2020
我将字符串列表转换为日期时间
first_test = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in first_test]
handler_arrive = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in handler_arrive]
然后我绘制它们:
scat1 = ax.scatter(first_test, handlers)
scat2 = ax.scatter(handler_arrive, handlers)
我尝试通过调用来连接它们:
plt.plot_date(first_test, handler_arrive)
还有这个
plt.plot(first_test, handler_arrive, '-o')
我收到此错误
File "C:\Users\user\PycharmProjects\handler_enablement\venv\lib\site-packages\matplotlib\dates.py", line 342, in _from_ordinalf
dt = dt.astimezone(tz)
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'UnitData'
所以我尝试plt.plot_date(first_test, handler_arrive, tz=None)(试图消除错误)没有工作
我从来没有设置过时区,也不需要设置时区,因为我以前在搞乱日期时间时从未遇到过这个错误,所以我知道为什么现在会发生这个错误。这一定是我试图将这些点与该函数调用联系起来的方式。好像不太对……
我已经尝试过这些方法,但它们不起作用
pandas scatter plotting datetime
Matplotlib connect scatterplot points with line - Python
我错过了什么?
【问题讨论】:
-
您能否发布几行数据,以便我们尝试重新创建错误?
-
对不起!使用示例数据进行了编辑。
-
感谢您的数据。当我尝试
plt.plot_date(first_test, handler_arrive)时,我没有出错,但我在 x 轴上有first_test在 y 轴上有handler_arrive。我的理解是,您不是试图将两个日期相互绘制,而是试图在每个处理程序的两个点之间画一条线。我看看能不能找到解决办法。
标签: python python-3.x datetime matplotlib