Matplotlib的get\u标签不能与自定义字符串标签一起使用

2024-04-25 09:11:10 发布

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

我只想把“日期”中的每一个第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


Tags: 字符串importdatetimeasnp公司plt数字
2条回答

您尝试设置每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

不过,请注意,将日期标绘为datetimes会自动提供一些非常好的格式。你知道吗

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

以下应起作用:

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)

相关问题 更多 >