python - Matplotlib 的 get_ticklabels 不适用于自定义字符串标签

标签 python datetime matplotlib

我只想将“日期”中的每 50 个项目绘制为 x 轴上的勾号。当我注释掉“plt.xticks(xticks,dates)”行时,它工作正常(上图),但是当我尝试用实际日期字符串替换数字时,它会放置所有日期,而不是仅每 50 个日期(下图)。

有人可以告诉我我做错了什么吗?谢谢!

import matplotlib.pyplot as plt
from datetime import datetime, date
import numpy as np

vals = [10, 11, 10, 6, 7, 4, 4, 6, 2, 3, 11, 11, 8, 8, 6, 9, 8, 8, 6, 6, 5, 2, 3, 5, 2, 3, 5, 6, 4, 6, 4, 1, 3, 4, 5, 5, 5, 7, 4, 4, 3, 5, 4, 5, 6, 4, 5, 3, 2, 4, 4, 4, 2, 2, 1, 7, 4, 8, 5, 3, 1, 3, 4, 6, 3, 7, 3, 3, 4, 5, 8, 6, 4, 2, 2, 4, 3, 2, 6, 4, 9, 6, 6, 5, 14, 17, 13, 11, 5, 7, 11, 7, 9, 6, 3, 7, 5, 4, 5, 7, 5, 4, 3, 4, 4, 2, 1, 2, 2, 2, 1, 1, 3, 4, 2, 2, 1, 2, 2, 2, 1, 3, 1, 1, 2, 1, 1, 2, 5, 1, 2, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 4, 5, 4, 3, 3, 2, 4, 3, 3, 3, 2, 1, 1, 3, 5, 4, 4, 4, 5, 6, 5, 3, 2, 3, 2, 2, 2, 2, 1, 1, 1, 2, 3, 2, 2, 3, 3, 2, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 3, 8, 1]

dates =['05/01/2018', '05/02/2018', '05/03/2018', '05/04/2018', '05/05/2018', '05/06/2018', '05/07/2018', '05/08/2018', '05/09/2018', '05/10/2018', '05/11/2018', '05/12/2018', '05/13/2018', '05/14/2018', '05/15/2018', '05/16/2018', '05/17/2018', '05/18/2018', '05/19/2018', '05/20/2018', '05/21/2018', '05/22/2018', '05/23/2018', '05/24/2018', '05/25/2018', '05/26/2018', '05/27/2018', '05/28/2018', '05/29/2018', '05/30/2018', '05/31/2018', '06/01/2018', '06/02/2018', '06/03/2018', '06/04/2018', '06/05/2018', '06/06/2018', '06/07/2018', '06/08/2018', '06/09/2018', '06/10/2018', '06/11/2018', '06/12/2018', '06/13/2018', '06/14/2018', '06/15/2018', '06/16/2018', '06/17/2018', '06/18/2018', '06/19/2018', '06/20/2018', '06/21/2018', '06/22/2018', '06/23/2018', '06/24/2018', '06/25/2018', '06/26/2018', '06/27/2018', '06/28/2018', '06/29/2018', '06/30/2018', '07/01/2018', '07/02/2018', '07/03/2018', '07/04/2018', '07/05/2018', '07/06/2018', '07/07/2018', '07/08/2018', '07/09/2018', '07/10/2018', '07/11/2018', '07/12/2018', '07/13/2018', '07/14/2018', '07/15/2018', '07/16/2018', '07/17/2018', '07/18/2018', '07/19/2018', '07/20/2018', '07/21/2018', '07/22/2018', '07/23/2018', '07/24/2018', '07/25/2018', '07/26/2018', '07/27/2018', '07/28/2018', '07/29/2018', '07/30/2018', '07/31/2018', '08/01/2018', '08/02/2018', '08/03/2018', '08/04/2018', '08/05/2018', '08/06/2018', '08/07/2018', '08/08/2018', '08/09/2018', '08/10/2018', '08/11/2018', '08/12/2018', '08/13/2018', '08/14/2018', '08/15/2018', '08/16/2018', '08/17/2018', '08/18/2018', '08/19/2018', '08/21/2018', '08/22/2018', '08/23/2018', '08/24/2018', '08/25/2018', '08/26/2018', '08/27/2018', '08/28/2018', '08/29/2018', '08/30/2018', '08/31/2018', '09/01/2018', '09/06/2018', '09/07/2018', '09/08/2018', '09/09/2018', '09/10/2018', '09/11/2018', '09/12/2018', '09/13/2018', '09/14/2018', '09/15/2018', '09/16/2018', '09/17/2018', '09/18/2018', '09/19/2018', '09/20/2018', '09/21/2018', '09/22/2018', '09/23/2018', '09/24/2018', '09/25/2018', '09/26/2018', '09/27/2018', '09/28/2018', '09/29/2018', '09/30/2018', '10/01/2018', '10/02/2018', '10/03/2018', '10/04/2018', '10/05/2018', '10/06/2018', '10/07/2018', '10/08/2018', '10/09/2018', '10/10/2018', '10/11/2018', '10/12/2018', '10/13/2018', '10/14/2018', '10/15/2018', '10/16/2018', '10/17/2018', '10/18/2018', '10/19/2018', '10/20/2018', '10/21/2018', '10/22/2018', '10/23/2018', '10/24/2018', '10/25/2018', '10/28/2018', '10/29/2018', '10/30/2018', '10/31/2018', '11/01/2018', '11/02/2018', '11/03/2018', '11/04/2018', '11/05/2018', '11/06/2018', '11/07/2018', '11/08/2018', '11/09/2018', '11/10/2018', '11/11/2018', '11/12/2018', '11/13/2018', '11/14/2018', '11/15/2018', '11/16/2018', '11/22/2018', '11/23/2018', '11/25/2018', '11/26/2018', '11/27/2018', '11/29/2018', '12/01/2018', '12/02/2018', '12/05/2018']
    xticks = np.arange(len(vals))

    ax = plt.axes()
    ax.bar(xticks,vals)
    plt.xticks(rotation=90)
    plt.xticks(xticks, dates)
    for label in ax.xaxis.get_ticklabels()[::50]:
        label.set_visible(True)

    plt.show()

Here, works fine when the ticks are just the numbers and line is commented out.

Here, the selection of only every 50th tick does not work and all dates are shown when we add back the conversion from number to date

最佳答案

以下应该有效:

ax = plt.axes()

pos = np.arange(len(vals))
ax.bar(pos, vals)

ax.set_xticks(pos[::50])
ax.set_xticklabels(np.array(dates)[::50], rotation=90)

关于python - Matplotlib 的 get_ticklabels 不适用于自定义字符串标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53641430/

相关文章:

python - 如何在 matplotlib(python) 中为轴添加标题和标签?

python - Matplotlib:具有多种颜色的 y 轴标签

python - Django 使用数据库值修改 css/sass

python - 有没有办法让 Python 控制台应用程序在 Windows 的任务栏中闪烁?

python - 为什么使用: from sub-package import item statement doesn't work?

MySQL SELECT 仅从具有 DATETIME 的行中选择 AVAILABLE 月份

JavaScript UTC 日期转换

python - 将日期字符串数组转换为日期

python - 如何在 Windows 64 位上安装 ImageMagick 和 Anaconda?

python - 在 Django rest api 中查询最小和最大大小