use*_*145 2 python plot numpy matplotlib
看看这个例子:
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
x = []
d = dt.datetime(2013, 7, 4)
for i in range(30):
d = d+dt.timedelta(days=1)
x.append(d)
y = range(len(x))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gcf().autofmt_xdate()
plt.bar(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud)
代码在图中的x轴上写出日期,请参见下图.问题是日期会被堵塞,如图所示.如何使matplotlib只写出每五或十分之一的坐标?

您可以为以下内容指定interval参数DateLocator.例如interval=5,定位器在每个第5个日期放置刻度.此外,将方法放在autofmt_xdate()后面bar以获得所需的输出.
import datetime as dt
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
x = []
d = dt.datetime(2013, 7, 4)
for i in range(30):
d = d+dt.timedelta(days=1)
x.append(d)
y = range(len(x))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5))
plt.bar(x, y, align='center') # center the bars on their x-values
plt.title('DateLocator with interval=5')
plt.gcf().autofmt_xdate()
plt.show()
Run Code Online (Sandbox Code Playgroud)

随着interval=3你会得到每3日期打勾:
