python - Matplotlib trouble plotting x-labels -


having issues using set_xlim. (possibly because of datetime objects??)

here's code (doing in ipython notebook):

%matplotlib inline import matplotlib.pyplot plt import numpy np import datetime  date_list = [datetime.datetime(2015, 6, 20, 0, 0), datetime.datetime(2015, 6, 21, 0, 0), datetime.datetime(2015, 6, 22, 0, 0), datetime.datetime(2015, 6, 23, 0, 0), datetime.datetime(2015, 6, 24, 0, 0), datetime.datetime(2015, 6, 25, 0, 0), datetime.datetime(2015, 6, 26, 0, 0)] count_list = [11590, 10743, 27369, 31023, 30569, 31937, 30205]  fig=plt.figure(figsize=(10,3.5)) ax=fig.add_subplot(111)  width = 0.8  ticklocations = np.arange(7)  ax.set_title("turnstiles totals lexington station c/a a002 unit r051 6/20/15-6/26-15") ax.bar(date_list, count_list, width, color='wheat', edgecolor='#8b7e66', linewidth=4.0) ax.set_xticklabels(date_list, rotation = 315, horizontalalignment = 'left') 

this gives me:

enter image description here

but when try make space on leftmost , rightmost edges code:

ax.set_xlim(xmin=-0.6, xmax=0.6) 

i huge error (this bottom snippet):

    223         tz = _get_rc_timezone()     224     ix = int(x) --> 225     dt = datetime.datetime.fromordinal(ix)     226     remainder = float(x) - ix     227     hour, remainder = divmod(24 * remainder, 1)  valueerror: ordinal must >= 1 

any idea what's going on guys? thanks!

for various historical reasons, matplotlib uses internal numerical date format behind-the-scenes. actual x-values in data format, 0.0 jan 1st 1900, , difference of 1.0 corresponds 1 day. negative values aren't allowed.

the error you're getting because you're trying set x-limits include negative range. without negative number, though, range on jan. 1st, 1900.

regardless, sounds you're wanting isn't ax.set_xlim @ all. try ax.margins(x=0.05) add 5% padding in x-direction.

as example:

import matplotlib.pyplot plt import numpy np import datetime  count_list = [11590, 10743, 27369, 31023, 30569, 31937, 30205] date_list = [datetime.datetime(2015, 6, 20, 0, 0),              datetime.datetime(2015, 6, 21, 0, 0),              datetime.datetime(2015, 6, 22, 0, 0),              datetime.datetime(2015, 6, 23, 0, 0),              datetime.datetime(2015, 6, 24, 0, 0),              datetime.datetime(2015, 6, 25, 0, 0),              datetime.datetime(2015, 6, 26, 0, 0)]  fig, ax = plt.subplots(figsize=(10,3.5)) ax.set_title("turnstiles totals lexington station c/a a002 unit r051 "              "6/20/15-6/26-15")  # difference align kwarg: i've centered bars on each date ax.bar(date_list, count_list, align='center', color='wheat',        edgecolor='#8b7e66', linewidth=4.0)  # rotates x-tick labels. have done # "fig.autofmt_xdate(rotation=315, ha='left')" match had. fig.autofmt_xdate()  # add padding you're after. 5% of data limits. ax.margins(x=0.05)  plt.show() 

enter image description here

note if wanted expand x-limits 0.6 in each direction, you'd like:

xmin, xmax = ax.get_xlim() ax.set_xlim([xmin - 0.6, xmax + 0.6]) 

however, ax.margins(percentage) easier long you're okay "padding" being in terms of ratio of current axes limits.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -