Matplot lib图的x轴上的刻度线过多的怪异问题

时间:2019-03-02 21:46:02

标签: python matplotlib

我正在绘制折线图,​​如下所示:

plt.plot(xs, ys)    
plt.show()

其中xs是代表日期的字符串列表,例如:

['2015-01-01 00:00:00', '2015-01-15 00:00:00', '2015-01-29 00:00:00', '2015-02-12 00:00:00', '2015-02-26 00:00:00', '2015-03-12 00:00:00', '2015-03-26 00:00:00', '2015-04-09 00:00:00', '2015-04-23 00:00:00', '2015-05-07 00:00:00', '2015-05-21 00:00:00', '2015-06-04 00:00:00', '2015-06-18 00:00:00', '2015-07-02 00:00:00', '2015-07-16 00:00:00', '2015-07-30 00:00:00', '2015-08-13 00:00:00', '2015-08-27 00:00:00', '2015-09-10 00:00:00', '2015-09-24 00:00:00', '2015-10-08 00:00:00', '2015-10-22 00:00:00', '2015-11-05 00:00:00', '2015-11-19 00:00:00', '2015-12-03 00:00:00', '2015-12-17 00:00:00', '2015-12-31 00:00:00', '2016-01-14 00:00:00', '2016-01-28 00:00:00', '2016-02-11 00:00:00', '2016-02-25 00:00:00', '2016-03-10 00:00:00', '2016-03-24 00:00:00', '2016-04-07 00:00:00', '2016-04-21 00:00:00', '2016-05-05 00:00:00', '2016-05-19 00:00:00', '2016-06-02 00:00:00', '2016-06-16 00:00:00', '2016-06-30 00:00:00', '2016-07-14 00:00:00', '2016-07-28 00:00:00', '2016-08-11 00:00:00', '2016-08-25 00:00:00', '2016-09-08 00:00:00', '2016-09-22 00:00:00', '2016-10-06 00:00:00', '2016-10-20 00:00:00', '2016-11-03 00:00:00', '2016-11-17 00:00:00', '2016-12-01 00:00:00', '2016-12-15 00:00:00', '2016-12-29 00:00:00', '2017-01-12 00:00:00', '2017-01-26 00:00:00', '2017-02-09 00:00:00', '2017-02-23 00:00:00', '2017-03-09 00:00:00', '2017-03-23 00:00:00', '2017-04-06 00:00:00', '2017-04-20 00:00:00', '2017-05-04 00:00:00', '2017-05-18 00:00:00', '2017-06-01 00:00:00', '2017-06-15 00:00:00', '2017-06-29 00:00:00', '2017-07-13 00:00:00', '2017-07-27 00:00:00', '2017-08-10 00:00:00', '2017-08-24 00:00:00', '2017-09-07 00:00:00', '2017-09-21 00:00:00', '2017-10-05 00:00:00', '2017-10-19 00:00:00', '2017-11-02 00:00:00', '2017-11-16 00:00:00', '2017-11-30 00:00:00', '2017-12-14 00:00:00', '2017-12-28 00:00:00', '2018-01-11 00:00:00', '2018-01-25 00:00:00', '2018-02-08 00:00:00', '2018-02-22 00:00:00', '2018-03-08 00:00:00', '2018-03-22 00:00:00', '2018-04-05 00:00:00', '2018-04-19 00:00:00', '2018-05-03 00:00:00', '2018-05-17 00:00:00', '2018-05-31 00:00:00', '2018-06-14 00:00:00', '2018-06-28 00:00:00', '2018-07-12 00:00:00', '2018-07-26 00:00:00', '2018-08-09 00:00:00', '2018-08-23 00:00:00', '2018-09-06 00:00:00', '2018-09-20 00:00:00', '2018-10-04 00:00:00', '2018-10-18 00:00:00', '2018-11-01 00:00:00', '2018-11-15 00:00:00', '2018-11-29 00:00:00', '2018-12-13 00:00:00', '2018-12-27 00:00:00']

和ys是代表分数的一堆int值,例如:

[70883, 63106, 76973, 71973, 54580, 64623, 67127, 81052, 41652, 58521, 57382, 45382, 68905, 77474, 68977, 68644, 93629, 88714, 108090, 130869, 129094, 114523, 110771, 132210, 110044, 140998, 157442, 188342, 156869, 179707, 203507, 240942, 262423, 278120, 260656, 314315, 273946, 365689, 343251, 299117, 373867, 463917, 625699, 589657, 565107, 615107, 552405, 659275, 910939, 535982, 699631, 853283, 564078, 903231, 1046810, 1020451, 1047713, 707396, 810879, 607520, 569610, 854798, 765915, 686773, 680880, 608677, 800183, 698931, 510455, 564391, 513260, 605777, 556326, 666129, 724712, 676530, 570635, 633332, 507160, 565754, 681522, 481315, 403914, 364243, 365601, 318114, 284698, 372140, 250942, 272253, 452179, 340992, 359701, 322556, 301214, 431651, 313142, 352983, 31275, 48839, 37165, 27091, 31378, 28689, 33870]

Tis非常好,但是在绘制时,x轴上有一小笔刻度线,而且不清晰。所以我想减少x轴上的刻度数。我尝试在plt.show()调用之前自行添加以下两行:

plt.gca().set_xticks(plt.gca().get_xticks()[::15])
plt.gca().xaxis.major.locator.set_params(nbins=5)

据我了解,这些行中的第一行应在图表上每15个滴答中使用1个,第二行应将其更改为只有6个滴答。这两个选项都减少了滴答声的数量,但是它们只显示前几个日期,例如:

['2015-01-01 00:00:00', '2015-01-15 00:00:00', '2015-01-29 00:00:00', '2015-02-12 00:00:00', '2015-02-26 00:00:00']

作为底部的标签。这就像我将它设置为使用较少的滴答声,这很好,但是它只使用前n个滴答声,而我希望它使用整个集合中均匀间隔的滴答声。

0 个答案:

没有答案