<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hi Steve,</div><div class=""><br class=""></div><div class="">When it comes to bar plots, Pandas doesn’t pass a datetime object to matplotlib  (it’s an old issue).</div><div class=""><br class=""></div><div class="">That’s the reason for the error when attempting to format a date  - it isn’t one.</div><div class=""><br class=""></div><div class="">Easiest solution is to plot directly with matplotlib</div><div class=""><br class=""></div><div class="">In your notation below that would be: (nothing - you don’t need to pass the returned axes back to anything)</div><div class=""><br class=""></div><div class="">fmt = mdates.DateFormatter('%Y’)</div><div class=""><br class=""></div><div class="">for i,col in enumerate(df):</div><div class=""><br class=""></div><div class=""><div class=""><span class="Apple-tab-span" style="white-space:pre">    </span>axs[i,0].bar(df.index,  df[col])</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">      </span>axs[i,0].xaxis.set_major_formatter(fmt)</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Though it might be easier to read to omit I, and  zip the two elements together and loop over them like this::</div><div class=""><br class=""></div><div class="">fig, axs = plt.subplots(9,2,figsize=(20,20),sharex=True)</div><div class=""><br class=""></div><div class="">for col, ax in zip(df.columns, axs.flatten()):</div><div class=""><span class="Apple-tab-span" style="white-space:pre">    </span>ax.bar(df.index,df[col])</div><div class=""><span class="Apple-tab-span" style="white-space:pre">  </span>ax.xaxis.set_major_formatter(fmt)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">That will give you horizontal, rather than vertical text as well.</div><div class=""><br class=""></div><div class="">If you need to rotate to fit all the years in use,</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">      </span>ax.tick_params(labelrotation=30, axis=‘x)   #or whatever angle you want</div><div class=""><br class=""></div><div class=""> Or use a locator to specific what you want</div><div class=""><br class=""></div><div class="">cheers</div><div class="">Chris</div><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 9 May 2018, at 11:06 pm, Steve Aplin <<a href="mailto:aplin.steve@gmail.com" class="">aplin.steve@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">Hello everyone,<br class=""><br class=""></div>Having a bit of an adventure controlling the display of the x axis when plotting a pandas dataframe or series using pandas plot(), as in df.plot(kind='bar') or s.plot(kind='bar').<br class=""><br class=""></div>Here's the head of one column:<br class=""><pre class="">1996-01-01    180.0
1997-01-01    159.0
1998-01-01    159.0
1999-01-01    152.0
2000-01-01    121.0
Name: column A, dtype: float64</pre><br class=""></div>... and here's its index:<br class=""><pre class="">DatetimeIndex(['1996-01-01', '1997-01-01', '1998-01-01', '1999-01-01',
               '2000-01-01', ... ],
              dtype='datetime64[ns]', freq=None)</pre>I want to show a bunch of subplots, so my code looks like this:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">from datetime import datetime<br class="">import matplotlib.pyplot as plt<br class="">import pandas as pd<br class="">import numpy as np<br class="">import matplotlib.dates as mdates<br class="">%matplotlib inline<br class=""><br class="">fig, axs = plt.subplots(9,2,figsize=(20,20),sharex=True)<br class="">for i,col in enumerate(df):<br class="">    ax = df[col].plot(kind='bar',ax=axs[i,0])<br class=""></blockquote></div><blockquote class="">   etc.<br class=""></blockquote><br class=""></div>... which gives an x tick value for each index item (i.e. each bar), in full datetime format (YYYY-MM-DD 00:00:00), rotated 90 degrees.<br class=""><br class=""></div>I don't want full datetime format, I just want the year portion. Googling the issue led me to trying this:<br class=""><br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">fig.autofmt_xdate()<br class=""></blockquote><div class=""><div class=""><div class=""><div class=""><div class=""><br clear="all" class=""><div class=""><div class=""><div class=""><div class=""><div class="gmail_signature"><div class="">... which rotates the labels, but still in full datetime format.<br class=""><br class=""></div><div class="">So I tried adding<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">formatter = mdates.DateFormatter('%Y')<br class="">ax.xaxis.set_major_formatter(formatter)<br class=""></blockquote><br class=""></div><div class="">which throws this error:<br class=""><pre class=""><span class="gmail-ansi-red-fg">ValueError</span>: DateFormatter found a value of x=0, which is an illegal date.  This usually occurs because you have not informed the axis that it is plotting dates, e.g., with ax.xaxis_date()</pre>... which is a bit confusing, since the index is a pandas datetimeindex.<br class=""><br class=""></div><div class="">Any ideas about how I could get my xaxis to not show just the year portion?<br class=""><br class=""></div><div class="">Thanks in advance!<br class=""></div><div dir="ltr" class=""><br class="">Stephen E. Aplin<div class="">Ottawa Canada</div></div></div></div>
</div></div></div></div></div></div></div></div></div>
_______________________________________________<br class="">Matplotlib-users mailing list<br class=""><a href="mailto:Matplotlib-users@python.org" class="">Matplotlib-users@python.org</a><br class="">https://mail.python.org/mailman/listinfo/matplotlib-users<br class=""></div></blockquote></div><br class=""></body></html>