擅长:python、mysql、java
<p>如果要将图形显示为分类条形图,即独立于实际日期的等距条形图,可以重新格式化xticklabels</p>
<pre><code>f = lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S').strftime('%b')
ax.set_xticklabels([ f(x.get_text()) for x in ax.get_xticklabels()])
</code></pre>
<p>其中<code>%b</code>是月份的缩写,ax是绘图的轴。</p>
<p>完整示例:</p>
<pre><code>import pandas as pd
import datetime
import matplotlib.pyplot as plt
df = pd.DataFrame(index = [datetime.datetime(2016, 7, 2, 0, 0),
datetime.datetime(2016, 8, 6, 0, 0),
datetime.datetime(2016, 9, 13, 0, 0),
datetime.datetime(2016, 10, 26, 0, 0),
datetime.datetime(2016, 11, 2, 0, 0)],
data = {'total' : [5, 3, 1, 0, 2]})
ax = df.plot(kind='bar')
f = lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S').strftime('%b')
ax.set_xticklabels([ f(x.get_text()) for x in ax.get_xticklabels()])
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/hXQvO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hXQvO.png" alt="enter image description here"/></a></p>