如何在 Matplotlib 中将时间作为索引值绘制为 Pandas 数据框?


要在 matplotlib 中将时间作为索引值绘制为 Pandas 数据框,我们可以采取以下步骤

步骤

  • 设置图形大小并调整子图之间的边距和周围边距。

  • 使用两列timespeed创建 Pandas 数据框。

  • 使用现有列设置 DataFrame 索引。

  • 要显示图形,请使用Show()方法。

示例

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

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Pandas dataframe
df = pd.DataFrame(dict(time=list(pd.date_range("2021-01-01 12:00:00", periods=10)), speed=np.linspace(1, 10, 10)))

# Set the dataframe index
df.set_index('time').plot()

# Display the plot
plt.show()

输出

它将产生以下输出 -

更新于: 2021 年 10 月 19 日

2K+ 查看

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.