如果我将值“TUESDAY”直接传递给WeekdayLocator,如下所示:
ax2.xaxis.set_major_locator(mdates.WeekdayLocator(TUESDAY))
我得到的结果是正确的。现在,当我尝试从下面的给定代码传递值时:
day = calendar.day_name[date.today().weekday()].upper()
fig, ax1 = plt.subplots()
ax1.grid(True)
ax2 = ax1.twinx()
lns1 = ax1.plot(date, y, marker='o')
plt.gcf().autofmt_xdate()
ax2.xaxis.set_major_locator(mdates.WeekdayLocator(day))
ax2.xaxis.set_major_formatter(DateFormatter('%b %d'))
plt.show()
我收到错误:
Traceback (most recent call last):
File "G:\py\weekly.py", line 53, in <module>
ax2.xaxis.set_major_locator(mdates.WeekdayLocator(day))
File "C:\Python34\lib\site-packages\matplotlib\dates.py", line 1250, in __init__
interval=interval, **self.hms0d)
File "C:\Python34\lib\site-packages\matplotlib\dates.py", line 721, in __init__
self._rrule = rrule(**self._construct)
File "C:\Python34\lib\site-packages\dateutil\rrule.py", line 591, in __init__
elif not wday.n or freq > MONTHLY:
AttributeError: 'str' object has no attribute 'n'
任何人都可以帮助我如何传递“天”mdates.WeekdayLocator()
中的值