Pandas dataframe plot time axis format total seconds into elapsed HH:MM:SS
Pandas dataframe plot time axis format total seconds into elapsed HH:MM:SS
我有一个 pandas 数据框,其中索引值是整数 0-10000,每个值对应 1 秒。我在数据框中有 1 temp
列。我可以使用 matplotlib 创建温度与时间的关系图:
import matplotlib.pyplot as plt
df.plot()
plt.show()
由于这个时间以秒为单位显示在 x 轴上(索引值),我想以更易读的方式显示时间,例如 50 分 5 秒,而不是 3005 秒。感谢您的帮助
index value are integers 0-10000 and each value corresponds to 1 second
转换索引 to_timedelta
并将 unit
指定为 seconds
:
df = pd.DataFrame({'temp': np.random.randn(10_001).cumsum()})
df.index = pd.to_timedelta(df.index, unit='seconds')
df.plot(figsize=(10, 3))
我有一个 pandas 数据框,其中索引值是整数 0-10000,每个值对应 1 秒。我在数据框中有 1 temp
列。我可以使用 matplotlib 创建温度与时间的关系图:
import matplotlib.pyplot as plt
df.plot()
plt.show()
由于这个时间以秒为单位显示在 x 轴上(索引值),我想以更易读的方式显示时间,例如 50 分 5 秒,而不是 3005 秒。感谢您的帮助
index value are integers 0-10000 and each value corresponds to 1 second
转换索引 to_timedelta
并将 unit
指定为 seconds
:
df = pd.DataFrame({'temp': np.random.randn(10_001).cumsum()})
df.index = pd.to_timedelta(df.index, unit='seconds')
df.plot(figsize=(10, 3))