1.横坐标设置时间格式
import matplotlib.pyplot as plt import matplotlib.dates as mdates # 配置横坐标为日期格式 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator())
例子:
from datetime import datetime import matplotlib.dates as mdates import matplotlib.pyplot as plt # 生成横纵坐标信息 dates = ['01/02/1991', '01/03/1991', '01/04/1991'] xs = [datetime.strptime(d, '%m/%d/%Y').date() for d in dates] ys = range(len(xs)) # 配置横坐标 plt.<a>本文来源gao($daima.com搞@代@#码(网</a>gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) # Plot plt.plot(xs, ys) plt.gcf().autofmt_xdate() # 自动旋转日期标记 plt.show()
2.设置日期坐标轴主副刻度值
所有坐标轴日期格式类型
MinuteLocator: locate minutes(f)
HourLocator: locate hours
DayLocator: locate specified days of the month
WeekdayLocator: Locate days of the week, e.g., MO, TU
MonthLocator: locate months, e.g., 7 for july
YearLocator: locate years that are multiples of base
RRuleLocator: locate using a matplotlib.dates.rrulewrapper. The rrulewrapper is a simple wrapper around adateutil.rrule (dateutil) which allow almost arbitrary date tick specifications. See rrule example.