Changing the xlim by date in Matplotlib

14,225

You need to call datetime.datetime.now() with the parentheses on the end, and for dstart, you need to use the datetime method of the datetime module: datetime.datetime(2015,4,1)

import datetime

datenow = datetime.datetime.now()
dstart = datetime.datetime(2015,4,1)

EDIT: To set the xticks to the first of the month (thanks to @AndyKubiak):

firsts=[]
for i in range(dstart.month, datenow.month+1):
    firsts.append(datetime.datetime(2015,i,1)) 
plt.xticks(firsts)
Share:
14,225
jenryb
Author by

jenryb

Updated on June 16, 2022

Comments

  • jenryb
    jenryb almost 2 years

    I am trying to make a visually appealing graph in Python. I was using Randal Olson's example http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/ here and trying to make some adjustments.

    My data is simple,

    dispute_percentage
    Out[34]: 
    
    2015-08-11    0.017647
    2015-08-12    0.004525
    2015-08-13    0.006024
    2015-08-14    0.000000
    2015-08-15    0.000000
    2015-08-17    0.000000
    

    The problem is that the data starts loading at Feb 2015, and I want to start displaying at April 2015.

    Here is my code

    from __future__ import division
    from collections import OrderedDict
    import pandas as pd
    from collections import Counter
    from pylab import * 
    import datetime as datetime 
    dispute_percentage.plot(kind = 'line')
    plt.xlabel('Date')
    plt.ylabel('Percent')
    plt.title('Percent Disputes In FY2015')
    
    # Remove the plot frame lines. They are unnecessary chartjunk.    
    ax = plt.subplot(111)    
    ax.spines["top"].set_visible(False)    
    ax.spines["bottom"].set_visible(False)    
    ax.spines["right"].set_visible(False)    
    ax.spines["left"].set_visible(False) 
    
    
    # Ensure that the axis ticks only show up on the bottom and left of the plot.    
    # Ticks on the right and top of the plot are generally unnecessary chartjunk.    
    ax.get_xaxis().tick_bottom()    
    #ax.get_yaxis().tick_left()    
    
    # Limit the range of the plot to only where the data is.    
    # Avoid unnecessary whitespace.   
    datenow = datetime.datetime.now
    dstart = datetime(2015,4,1)
    print datenow 
    plt.ylim(0, .14)    
    plt.xlim(dstart, datenow)    
    

    The xlim is what I am struggling with. I'm getting the error

    File "C:/Mypath/name.py", line 52, in <module>
        dstart = datetime(2015,4,1)
    
    TypeError: 'module' object is not callable
    

    If anyone could help with this that would be great. Also any input on trying to make it prettier would also be appreciated.

  • jenryb
    jenryb over 8 years
    Thanks for the answer. Is there a way to only list the first of the month? I am getting labels April 08, April 29, May 20, July 01, etc.
  • Andy Kubiak
    Andy Kubiak over 8 years
    firsts = []; for i in range(1, 13): firsts.append(datetime.datetime(2015, i, 1))
  • tmdavison
    tmdavison over 8 years
    @jenryb: See me edit, which uses the method from @AndyKubiak (but limits the xticks to your dstart to datenow range)
  • jenryb
    jenryb over 8 years
    @tom This worked well, thank you. I would just change "first" to "firsts" in the middle line and a parenthesis to the end of the middle line as well and it works perfectly.