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

时间:2018-12-05 22:01:42

标签: 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

2 个答案:

答案 0 :(得分:2)

以下方法应该起作用:

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)

答案 1 :(得分:1)

您尝试设置每第50个可见标签。但是其他所有内容仍然可见,因此所有内容以及您设置的内容均可见。当然,您可以设置一些可见的;但是也许更好的解决方案是根本不创建标签?

要在第50个条目上打勾并标记,您可以对打勾和标签列表进行切片。

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

vals = [10, 11, ....]
dates =['05/01/2018', '05/02/2018', ....]
xticks = np.arange(len(vals))

ax = plt.axes()
ax.bar(xticks,vals)

plt.xticks(xticks[::50], dates[::50], rotation=90)

plt.show()

enter image description here

但是请注意,仅将日期绘制为日期时间会自动为您提供非常好的格式。

import matplotlib.pyplot as plt
from datetime import datetime

vals = [10, 11, ..., 1]
dates =['05/01/2018', '05/02/2018', ..., '12/05/2018']
dates = [datetime.strptime(s, "%m/%d/%Y") for s in dates]

ax = plt.axes()
ax.bar(dates,vals)

plt.show()

enter image description here