发布于 2018-01-20 09:50:35
您可以通过ax.set_xticks()设置x轴使用的刻度,通过ax.set_xticklabels()设置标签。
例如,您可以只为该方法提供要使用的日期列表,例如当前pd.DataFrame索引(df.index[::20])的每20个值,然后将日期字符串的格式设置如下。
# Get the current axis
ax = plt.gca()
# Only label every 20th value
ticks_to_use = df.index[::20]
# Set format of labels (note year not excluded as requested)
labels = [ i.strftime("%-H:%M") for i in ticks_to_use ]
# Now set the ticks and labels
ax.set_xticks(ticks_to_use)
ax.set_xticklabels(labels)Notes
如果标签仍然重叠,还可以通过传递旋转参数(例如ax.set_xticklabels(labels, rotation=45))来旋转它们。
这里有一个关于时间字符串格式的有用参考:http://strftime.org。
发布于 2019-10-25 14:52:56
我的情节也面临着类似的问题。

Matplotlib在轴上自动处理时间戳,但只有在时间戳格式时才自动处理。index中的时间戳是字符串格式的,因此我将read_csv更改为
pd.read_csv(file_path, index_col=[0], parse_dates=True)尝试将索引更改为时间戳格式。这为我解决了问题,希望对你也一样。

https://stackoverflow.com/questions/48352482
复制相似问题