在x轴上显示最后N个值作为Python中matplotlib中的标签范围

问题描述 投票:0回答:3

在我的python脚本中,df.Value具有n个值的集合(200个)。我需要最后100个值作为我的x轴标签,例如最近100-200个索引值。

plt.figure(figsize=(100, 5), dpi=100)
plt.plot(df['Time'], df['sale'], label='sales')
plt.xlabel('Time ')
plt.ylabel('sales')
plt.title('sales')
plt.legend()
plt.show()

它在x轴上显示0-200的值,但我需要在x轴标签中显示最后N个值样本数据

样本数据

销售和时间

  • 1 604.802656 13:00:00
  • 2 604.400000 13:01:00
  • 3 604.900024 13:02:00
  • 4 604.099976 13:03:00
  • 5 604.000000 13:04:00
  • 6 604.250000 13:05:00
  • 7 604.400024 13:06:00
  • 8 604.150024 13:07:00
  • 9 604.000000 13:08:00
python matplotlib matlab-figure
3个回答
1
投票
plt.xticks(np.arange(100),df['Time'].values[100:200])

这将帮助您在最近的100个值中显示100个轴标签


0
投票

尝试一下

plt.xticks(np.arange(100, 200, step=1))

对于您的情况,即在x轴上的时间,您可以看到此信息https://stackoverflow.com/a/16428019/5202279


0
投票
plt.figure(figsize=(100, 5), dpi=100)
plt.plot(df['Time'], df['sale'], label='sales')
plt.xlabel('Time ')
plt.xticks(np.arrange(100),df.Time[100:200],rotation=45)
plt.ylabel('sales')
plt.title('sales')
plt.legend()
plt.show()
  • np.ararange(100)表示要显示的100 X轴值
  • 和df.Time [100:200]从数据集df.Time中获取最后100个字符串值
  • 将标签旋转45度

感谢您的支持

© www.soinside.com 2019 - 2024. All rights reserved.