gpt4 book ai didi

python - 将 x 轴上的秒数转换为最小值 :sec,,同时保持相同的 x 限制

转载 作者:行者123 更新时间:2023-12-03 08:31:00 25 4
gpt4 key购买 nike

我正在绘制随时间变化的值,以秒为单位。但是,我想在 x 轴上显示以分钟为单位的时间:秒。我现在已经这样做了,但我得到了像 0.8 这样的小数,这是你在时间表示法中不想要的。我尝试使用 '{}:{}.format(mins, secs)' 修复此问题,但随后我的 x 值变成字符串,并且我无法再获得适当的 x lim。

有人知道一种将秒转换为分钟:秒的好方法,同时能够保持相同的 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)')

Example

最佳答案

如果这对你来说不是问题,我通常使用 pandas 转换为日期时间

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

enter image description here

关于python - 将 x 轴上的秒数转换为最小值 :sec,,同时保持相同的 x 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65109447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com