pandas: How to format timestamp axis labels nicely in df.plt()?
<p>I have a dataset that looks like this:</p>

<pre><code>          prod_code      month  items      cost
0  040201060AAAIAI 2016-05-01      5    572.20 
1  040201060AAAKAK 2016-05-01    164  14805.19  
2  040201060AAALAL 2016-05-01  13465  14486.07   
</code></pre>

<p>Doing <code>df.dtypes</code> shows that the <code>month</code> column is a <code>datetime64[ns]</code> type. </p>

<p>I am now trying to plot the cost per month for a particular product: </p>

<pre><code>df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].plot()
plt.show()
</code></pre>

<p>This works, but the x-axis isn't a timestamp as I'd expect:</p>

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

<p>How can I format the x-axis labels nicely, with month and year labels?</p>

<p>Update: I also tried this, to get a bar chart, which does output timestamps on the x-axis, but in a very long unwieldy format: </p>

<pre><code>df[df.bnf_code=='040201060AAAIAI'].plot.bar(x='month', y='cost', title='Spending on 040201060AAAIAI')
</code></pre>

<p>If you set the dates as index, the x-axis should be labelled properly:</p>

<pre><code>df[df.bnf_code=='040201060AAAIAI'][['month', 'cost']].set_index('month').plot()
</code></pre>

<p>I have simply added <code>set_index</code> to your code.</p>

python pandas matplotlib