Python和Matplotlib:减少x记号的数量并删除零填充

2024-05-12 21:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是matplotlib和pyplot的新手,尝试绘制一个大型数据集。下面是一个小片段。

情节很成功,但X记号很拥挤。

如何减少刻度线的数量?

使用plt.locator_params(nbins=4)返回错误:

AttributeError: 'FixedLocator' object has no attribute 'set_params'

是否有方法从pyplot中的日期标签中移除0填充?

import matplotlib.pyplot as plt


x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
xticks = ['01/01', '01/02', '01/03', '01/04', '01/05', '01/06', '01/07', '01/08', '01/09', '01/10', '01/11', '01/12', '01/13', '01/14', '01/15', '01/16', '01/17', '01/18', '01/19', '01/20', '01/21', '01/22', '01/23', '01/24', '01/25', '01/26', '01/27', '01/28', '01/29', '01/30']
y = [80, 80, 60, 30, 90, 50, 200, 300, 200, 150, 10, 80, 20, 30, 40, 150, 160, 170, 180, 190, 20, 210, 220, 20, 20, 20, 200, 270, 280, 90, 00]
y2 = [100, 100, 200, 300, 40, 50, 60, 70, 80, 90, 100, 110, 12, 13, 10, 110, 16, 170, 80, 90, 20, 89, 28, 20, 20, 28, 60, 70, 80, 90, 30]


plt.plot(x, y)
plt.plot(x, y2)
plt.xticks(x, xticks, rotation=90)
plt.show()

enter image description here


Tags: 数据数量plotmatplotlib绘制pltparamspyplot