首页 > 解决方案 > Set xticks interval with date values

问题描述

Hello I have a a list that has about 150 dates that are stored in string format. I would like to set an interval so that there are only 10 ticks along the x-axis I am not sure how to do this without changing the type format.

'1980-06',
'1980-09',
'1980-12',
'1981-03',
'1981-06',
'1981-09',
'1981-12',
...

标签: pythonmatplotlib

解决方案


您可以提供要显示的刻度列表。要获得该刻度列表,只需将您的日期列表分成十部分,然后找出每个刻度应该有多远。然后使用 Python 的索引来获取刻度的值。检查以下:

import math
dates = ['1980-06', '1980-09', '1980-12', '1981-12' ...] # Your date list
date_len = len(dates)
step = int(math.ceil(date_len/10))
ticks = dates[::step] # ticks to show on graph

推荐阅读