Matplotlib: align xtick labels
<p>I have generated the following plot using the code below. I would like to position the <code>xtick</code> labels at appropriate place. Currently, it is misleading and the first label <code>12/06</code> is missing.</p>

<p><a href="http://i.stack.imgur.com/HBYbL.png" rel="nofollow"><img src="http://i.stack.imgur.com/HBYbL.png" alt="enter image description here"></a></p>

<p>The code is as follows:</p>

<pre><code>import matplotlib.pyplot as plt
import datetime as dt
import matplotlib.dates as mdates

list_date = 
['2016-06-12',
 '2016-06-13',
 '2016-06-14',
 '2016-06-15',
 '2016-06-16',
 '2016-06-17',
 '2016-06-18',
 '2016-06-19',
 '2016-06-20',
 '2016-06-21',
 '2016-06-22',
 '2016-06-23',
 '2016-06-24',
 '2016-06-25',
 '2016-06-26',
 '2016-06-27',
 '2016-06-28',
 '2016-06-29',
 '2016-06-30',
 '2016-07-01',
 '2016-07-02',
 '2016-07-03',
 '2016-07-04',
 '2016-07-05',
 '2016-07-06',
 '2016-07-07',
 '2016-07-08',
 '2016-07-09']

list_count = 
[9490,
 595442,
 566104,
 664133,
 655221,
 612509,
 607395,
 597703,
 613051,
 635764,
 705905,
 535869,
 583516,
 630818,
 641495,
 697591,
 578071,
 547280,
 561775,
 581784,
 594175,
 552944,
 545131,
 493400,
 604280,
 510182,
 518883,
 413648]

date = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in list_date]

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=2))
plt.bar(date, list_count, align = 'center')
plt.gcf().autofmt_xdate()
plt.grid()
plt.xlabel("Period from  12/06/2016 to 09/07/2016")
plt.ylabel("Daily hits")
plt.title("Count of daily visitors")
plt.show()
</code></pre>

<p>You can customize the position of the x-ticks by explicitly passing a list, e.g. to mark every date in your list with a tick, you would do</p>

<pre><code>plt.gca().xaxis.set_ticks(date)
</code></pre>

<p>instead of</p>

<pre><code>plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=2))
</code></pre>

<p>to avoid over-dense ticks, you could pass e.g. <code>date[::2]</code>.</p>

python python-2.7 matplotlib data-visualization