【问题标题】:Show xticks with specified interval on pandas DataFrame在 pandas DataFrame 上显示具有指定间隔的 xticks
【发布时间】:2016-03-19 01:42:12
【问题描述】:

当我绘制以下名为 df 的 DataFrame 时,我得到了这个数字。

df = pd.DataFrame([[1 for _ in range(20)]]).T
df.index = ["{}:00:00".format(x) for x in range(20)]
df.plot()

我想每隔两三行显示xticks,即0:00、2:00、4:00,...因为每5个间隔看起来太大了。

是否可以按指定的时间间隔显示 xticks?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您的索引是一堆字符串,而不是实际日期。

    如果你使用日期,pandas 可以更聪明地告诉 matplotlib 做什么:

    df = pandas.DataFrame({'A': range(20)}, index=pandas.date_range('2012-01-01', freq='1H', periods=20))
    df.plot()
    

    也就是说,如果您仍然不喜欢 pandas 的选择,那么关于在 matplotlib 中放置刻度有很多问题。这是其中之一: Frequency and rotation of x-axis labels in Matplotlib

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2018-01-26
      • 2018-01-14
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      相关资源
      最近更新 更多