【问题标题】:Trying to connect scatter plotted datetimes gives me tz error from matplot尝试连接散点图日期时间给我来自 matplot 的 tz 错误
【发布时间】: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


【解决方案1】:

感谢您发布您的数据。以下是我基于制表符分隔版本创建的数据框:

    handler first_test  handler_arrived
0   hvvm-02 7/15/2020   1/26/2020
1   hvvm-03 4/8/2020    1/26/2020
2   hvvm-04 5/27/2020   1/26/2020
3   hvvm-07 7/8/2020    1/26/2020
4   hvvm-09 2/26/2020   1/7/2020
5   hvvm-10 2/26/2020   1/7/2020
6   hvvm-11 7/1/2020    1/6/2020
7   hvvm-12 7/15/2020   1/6/2020

然后,我主要使用您的代码创建了用于绘图的数据系列:

handlers = df['handler']
first_test = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in df['first_test']]
handler_arrive = [dt.datetime.strptime(date, '%m/%d/%Y').date() for date in df['handler_arrived']]

当我使用plt.plot_date(first_test, handler_arrive) 绘制散点图时,我没有遇到任何错误。 (delta: xx days) 部分会不会是绘图中的复杂因素?

我还使用以下代码绘制了连接两组日期的图:

plt.rcdefaults()
fig, ax = plt.subplots()
scat1 = ax.scatter(first_test, handlers)
scat2 = ax.scatter(handler_arrive, handlers)
for i in range(len(first_test)):     
    plt.plot([first_test[i],handler_arrive[i]], [handlers[i],handlers[i]], color='black')

结果如下:

参考请见Matplotlib python connect two scatter plots with lines for each pair of (x,y) values?

希望对你有帮助!

【讨论】:

  • 您的 for 循环就是答案! plt.plot([first_test[i],handler_arrive[i]], [handlers[i],handlers[i]], color='black') 我感觉我没有正确访问线路。当我查看 plot 函数时,它并没有给我太多关于参数的信息,因此您的代码似乎沿着每个处理程序的 y 轴迭代到图中的每个日期。那是对的吗?再次感谢您的回答!在吃我。
  • 是的。似乎plt.plot 绘制的不仅仅是线条 - 它还在散点图中绘制了点。我认为你所描述的是正确的。 plt.plot 接受一对 x 轴坐标和一对 y 轴坐标,而这对 y 轴坐标是相同的坐标,这就是我们想要的。老实说,我也不知道这个,所以谢谢你提出这个问题,我今天学到了新东西!
  • 哈哈,那我们都学会了! :) 要补充一点,在这种情况下,y 坐标集是相同的,但在其他情况下它们可能不同?我想不出情况,但我必须测试一下。
猜你喜欢
  • 2018-03-05
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
相关资源
最近更新 更多