python - Changing the xlim by date in Matplotlib -
i trying make visually appealing graph in python. using randal olson's example http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/ here , trying make adjustments.
my data 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 data starts loading @ feb 2015, , want start displaying @ april 2015.
here code
from __future__ import division collections import ordereddict import pandas pd collections import counter pylab import * import datetime datetime dispute_percentage.plot(kind = 'line') plt.xlabel('date') plt.ylabel('percent') plt.title('percent disputes in fy2015') # remove plot frame lines. 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 axis ticks show on bottom , left of plot. # ticks on right , top of plot unnecessary chartjunk. ax.get_xaxis().tick_bottom() #ax.get_yaxis().tick_left() # limit range of plot 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 struggling with. i'm getting error
file "c:/mypath/name.py", line 52, in <module> dstart = datetime(2015,4,1) typeerror: 'module' object not callable if great. input on trying make prettier appreciated.
you need call datetime.datetime.now() parentheses on end, , dstart, need use datetime method of datetime module: datetime.datetime(2015,4,1)
import datetime datenow = datetime.datetime.now() dstart = datetime.datetime(2015,4,1) edit: set xticks first of month (thanks @andykubiak):
firsts=[] in range(dstart.month, datenow.month+1): firsts.append(datetime.datetime(2015,i,1)) plt.xticks(firsts)
Comments
Post a Comment