Matplotlib的get_ticklabels不适用于自定义字符串标签 [英] Matplotlib's get_ticklabels not working with custom string labels

查看:45
本文介绍了Matplotlib的get_ticklabels不适用于自定义字符串标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将日期"中的每第50个项目绘制为x轴上的刻度.当我注释掉"plt.xticks(xticks,dates)"行时,它可以正常工作(上图),但是当我尝试用实际的日期字符串替换数字时,它将所有日期而不是每第50个日期放入(下图).

I would like to plot only every 50th item from "dates" as a tick on the x axis. When I comment out the line "plt.xticks(xticks, dates)", it works fine (top figure), but when I attempt to replace the numbers with the actual date strings, it puts ALL the dates instead of only every 50th ones (bottom figure).

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

Can someone tell me what I'm doing wrong? Thanks!

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()

推荐答案

以下方法应该起作用:

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)

这篇关于Matplotlib的get_ticklabels不适用于自定义字符串标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆