def format_xaxis(fig):
# here I am setting the major ticks to each decade
# Change to something appropriate for your data
major= dates.MonthLocator(bymonthday=1)
#Here I am setting each minor ticks to mid-decade
# Change to something appropriate for your data
minor=dates.MonthLocator(bymonthday=15)
#Here I am setting each major ticks and minor tick formatting
# Change to something appropriate for your data
#look at http://strftime.org/ for other options
majorfmt = dates.DateFormatter('%B')
minorfmt = dates.DateFormatter('%d')
# Set the locators and formats for all of the subplots axes
[i.xaxis.set_major_locator(major) for i in fig.axes]
[i.xaxis.set_minor_locator(minor) for i in fig.axes]
[i.xaxis.set_major_formatter(majorfmt) for i in fig.axes]
[i.xaxis.set_minor_formatter(minorfmt) for i in fig.axes]
# Here I am offsetting the major ticks down so they dont overlap with minor tick lables
[i.get_xaxis().set_tick_params(which='major', pad=15) for i in fig.axes]
#make them look nice
for t in fig.axes:
for tick in t.xaxis.get_major_ticks():
tick.label1.set_horizontalalignment('center')
for label in t.get_xmajorticklabels() :
label.set_rotation(0)
label.set_weight('bold')
for label in t.xaxis.get_minorticklabels():
label.set_fontsize('small')
如果您想控制主要和次要记号的位置和格式,您需要使用date tickers。以下是我经常用于绘制跨多个月的时间序列的函数:
您可以用
dates.MonthLocator
交换适合您的数据的任何定位器。看看我提供的链接。你知道吗举个小例子:
相关问题 更多 >
编程相关推荐