【问题标题】:Set matplotlib grid ticks based on specific dates根据特定日期设置 matplotlib 网格刻度
【发布时间】:2015-12-21 19:39:46
【问题描述】:

我正在绘制 30 年的 Pandas 系列日期时间。 x 轴是日期:

Datetime
1965-06-08    3545
1965-06-09    6378 
1965-06-10    9857
1965-06-11    2528
....
Freq: W-SUN, dtype: int64

我希望每个月都有一个“次要刻度”,每年都有一个“主要刻度”。

因为这是三十年的数据,手动放置刻度是行不通的。

基于 matplotlib 的日期 API,http://matplotlib.org/api/dates_api.html

是否可以在每个月设置一个刻度?

如果我要手动设置网格,我会这样做:

import numpy as np                                                               
import matplotlib.pyplot as plt                                                                                                                                 
fig = plt.figure()                                                               
ax = fig.add_subplot(1,1,1)                                                      

# This is where I manually set ticks. Can I use Datetime data instead???                                     
major_ticks = np.arange(0, 101, 20)                                              
minor_ticks = np.arange(0, 101, 5)                                               

ax.set_xticks(major_ticks)                                                       
ax.set_xticks(minor_ticks, minor=True)                                           
ax.set_yticks(major_ticks)                                                       
ax.set_yticks(minor_ticks, minor=True)                                           
ax.grid(which='both')                                                                                           
ax.grid(which='minor', alpha=0.3)                                                
ax.grid(which='major', alpha=0.4)                                                
plt.show()

【问题讨论】:

    标签: python numpy pandas matplotlib


    【解决方案1】:

    你想要matplotlib.dates.YearLocatormatplotlib.dates.MonthLocator

    import matplotlib.pyplot as plt
    from matplotlib.dates import MonthLocator, YearLocator
    import numpy as np
    
    fig, ax = plt.subplots(1, 1)
    
    x = np.arange('1965-01', '1975-07', dtype='datetime64[D]')
    y = np.random.randn(x.size).cumsum()
    ax.plot(x, y)
    
    yloc = YearLocator()
    mloc = MonthLocator()
    ax.xaxis.set_major_locator(yloc)
    ax.xaxis.set_minor_locator(mloc)
    ax.grid(True)
    

    【讨论】:

    • 有什么方法可以将ax.xaxis.set_major_locator(yloc)plt 一起使用,即import matplotlib.pyplot as plt 然后plt.xaxis.set_major_locator()?还是必须设置ax?我从来没有理解过这种区别。
    • xaxismatplotlib.axes._subplots.AxesSubplot 实例的属性。假设您想更改当前的轴集,您可以使用 plt.setp(plt.gca().xaxis, 'major_locator', mloc) 之类的东西。不过,这很丑陋。我强烈建议养成使用面向对象接口的习惯,而不是尝试使用“plt.”函数调用来做所有事情——这将使您的代码更具可读性。
    • 同意。但是,我正在策划熊猫系列。使用 ax.xaxis.set_major_locator(yloc) 会引发以下 AttributeError:AttributeError: 'tuple' object has no attribute 'xaxis
    • @ali_m,我使用ax.xaxis.set_major_locator(mp.dates.DayLocator()) 每天设置网格线,但只显示年份。是否有显示 YYYY-MM-DD 的选项?
    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多