在 y 轴 matplotlib 折线图上绘制时间增量时差列表

社会演员多 python 239

原文标题Plotting time delta time difference list on y-axis matplotlib line graph

我正在尝试使用两个列表在 matplotlib 中绘制折线图。

我对 x 轴的第一个列表是

x = [1,2,3,4,5,6,7,8,9,10]

我对 y 轴的下一个列表是从 datetime 对象列的 pandas DF 计算每个 x 轴值的时间差。

我通过这样做得到了这份清单

    diff_layers_sd['Difference'] = diff_layers_sd['ValueTime'].diff()

这给了我一个填充了 timedelta 对象的列表

y = [Timedelta('0 days 00:04:31'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:02'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:01'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:16'), Timedelta('0 days 00:04:01'), Timedelta('0 days 00:04:16')]

我将此列转换为列表,然后尝试绘制它,但我的情节是空的。有没有办法绘制 Timedelta 或格式化我的数据?我希望它只是在 y 轴上的 MM:SS

代码:

fig, ax = plt.subplots(nrows=13, ncols=1, figsize=(20,20),sharex=False)
...
ax[i].plot(x,y, label=title)
ax[i].yaxis_date()
ax[i].yaxis.set_major_formatter(mdates.DateFormatter("%M:%S")

原文链接:https://stackoverflow.com//questions/71918841/plotting-time-delta-time-difference-list-on-y-axis-matplotlib-line-graph

回复

我来回复
  • BENY的头像
    BENY 评论

    我们能做的

    s = diff_layers_sd['ValueTime'].diff().dt.total_seconds()
    diff_layers_sd['Difference'] = (s//60).astype(str) +":" +(s%60).astype(str) 
    
    2年前 0条评论