【问题标题】:Convert seconds on x axis into min:sec, while keeping the same x limits将 x 轴上的秒数转换为 min:sec,同时保持相同的 x 限制
【发布时间】:2020-12-02 13:51:44
【问题描述】:

我正在绘制一段时间内的值,以秒为单位。但是,我想在 x 轴上以分钟:秒为单位显示时间。我现在已经这样做了,但是我得到了像 0.8 这样的小数,你不需要时间符号。我尝试用'{}:{}.format(mins, secs)' 修复它,但后来我的 x 值变成了字符串,我无法再获得适当的 x 限制了。

有没有人知道一种将秒转换为 min:secs 同时又能保持相同 xlims 的好方法?

import matplotlib.pyplot as plt


y = np.random.rand(90, 1)

print(y)
    
x = np.arange(1380, 1470, 1)    # seconds
x = x/60                        # minutes


plt.plot(x, y)
plt.xlabel('Time (mins)')

【问题讨论】:

  • 您是否尝试过转换为时间戳?我不确定 numpy,但 pandas 很容易处理,所以它在 numpy 中应该是相似的

标签: python numpy matplotlib time


【解决方案1】:

如果对你没问题的话,我一般都是用pandas转成datetime

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

y = np.random.rand(90, 1)
x = np.arange(1380, 1470, 1)    # seconds
x = x/60                        # minutes
t = pd.to_datetime(x, unit='m') # convert to datetime

fig = plt.figure()
plt.plot(t, y)
plt.xlabel('Time (mins)')
fig.autofmt_xdate() # auto format

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 2020-10-04
    • 2018-11-04
    • 2011-04-09
    • 2014-09-04
    • 2022-01-24
    • 2020-12-17
    相关资源
    最近更新 更多