matplotlib datetime xlabel issue

后端 未结 3 1933
离开以前
离开以前 2021-02-20 15:19

I\'m seeing some strange behavior in the x-axis auto-labeling for dates in matplotlib. When I issue the command:

from datetime import datetime as dt
plot( [ dt(2         


        
3条回答
  •  太阳男子
    2021-02-20 15:57

    It does feel like a bug; I would take it to the matplotlib mailing list and see what people there can say about it.

    One workaround I can provide is the following:

    from datetime import datetime as dt
    from matplotlib import pylab as pl
    
    fig = pl.figure()
    axes = fig.add_subplot(111)
    axes.plot( [ dt(2013, 1, 1), dt(2013, 5, 18)], [ 1 , 1 ], linestyle='None', marker='.')
    ticks = axes.get_xticks()
    n = len(ticks)//6
    axes.set_xticks(ticks[::n])
    fig.savefig('dateticks.png')
    

    Apologies for the OO approach (which isn't what you did), but that makes it a lot easier to tie the ticks to the plot. The number 6 is just the number of labels I want along the x-axis, and then I reduce the actual number of ticks that matplotlib came up with, by the calculated n.

提交回复
热议问题