gpt4 book ai didi

python - 使用 periodIndex 绘制 pandas 数据框

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:44 24 4
gpt4 key购买 nike

我在绘制具有 PeriodIndexpandas 数据框时遇到问题。

我的数据存在差距,我希望实现以下目标:

  • 间隙应保持间隙,不进行插值;
  • 由于每个值在整个时间段内都有效,因此该值应在整个时间段内显示为水平线。
  • 优点:周期边界处没有垂直线。

#示例

yearly = pd.DataFrame({'avSpeed': [50, 40, 20, 16]}, index=pd.PeriodIndex(['2014', '2015', '2018', '2019'], freq='A'))

avSpeed
2014 50
2015 40
2018 20
2019 16

到目前为止我的代码

  • 通过使用 NaN 填充间隙来使数据帧无间隙(通过以不变的频率重新采样):

      yearly2 = yearly.resample('A').mean()

    avSpeed
    2014 50.0
    2015 40.0
    2016 NaN
    2017 NaN
    2018 20.0
    2019 16.0
  • 使用 steps-post 绘制此图:

      plt.figure()
    yearly2['avSpeed'].plot(color='red', drawstyle='steps-post')

    enter image description here

还缺少什么:

  • 主要问题:2019 年的值仅在年初显示。
  • 此外:存在垂直连接线(不需要)。

最佳答案

您可以将 hlinespandas.PeriodIndex 属性 start_timeend_time 一起使用

ax = plt.subplot()
ax.hlines(yearly, yearly.index.start_time, yearly.index.end_time, 'r', label='avSpeed')
ax.legend()

enter image description here

关于python - 使用 periodIndex 绘制 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904964/

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