问题
绘制个例逐小时观测结果时,由于时间标签太长,因此再绘图时,选择了12H绘制标签:
fig2 = plt.figure(figsize=(8,8))#设置画布大小times=wrftimeax = fig2.add_subplot(1,1,1)ax.plot(times,var_plot[0:121],marker='.',linestyle='-', linewidth = 0.5, label='MOSAiC', color='black')#plt.plot(times,wrf_var[1:10],marker='o', markersize=5, linestyle='-', label='Polar-WRF', color='coral')font1 = {'family' : 'Times New Roman','weight' : 'bold','size' : 20,}plt.ylabel("upward longwave flux W/m^2",font1)plt.xlabel('Date',font1)plt.xlim(np.min(times), np.max(times))dates=pd.date_range(np.min(times), np.max(times),freq='12H')#ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d %H:%M'))plt.xticks(dates,fontproperties='Times New Roman', size=12,weight='bold')plt.yticks(fontproperties='Times New Roman', size=12,weight='bold')plt.legend(loc='upper center',frameon=False)plt.show()
看上去还行,但是不太容易看出日变化,所以想再给它的刻度弄密一点,即修改sep=1H,不过那样会造成刻度过于密集,所以研究了一下关于python Matplotlib里对于时间刻度的处理。
matplotlib.dates.DateFormatter——修改时间标签显示格式
这个功能还是比骄好用的,比如加上这行代码后,日期看上去清爽了很多:
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d %H:%M'))
但讲道理,把日期和时间放一起总觉得很不顺眼,我想要让它们分成两行,应该怎么做呢?
于是试着加入换行符/n
,发现是有效的:
fig2 = plt.figure(figsize=(8,8))#设置画布大小times=wrftimeax = fig2.add_subplot(1,1,1)ax.plot(times,var_plot[0:121],marker='.',linestyle='-', linewidth = 0.5, label='MOSAiC', color='black')#plt.plot(times,wrf_var[1:10],marker='o', markersize=5, linestyle='-', label='Polar-WRF', color='coral')font1 = {'family' : 'Times New Roman','weight' : 'bold','size' : 20,}plt.ylabel("upward longwave flux W/m^2",font1)plt.xlabel('Date',font1)plt.xlim(np.min(times), np.max(times))dates=pd.date_range(np.min(times), np.max(times),freq='12H')ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d\n%H:%M'))plt.xticks(dates,fontproperties='Times New Roman', size=12,weight='bold')plt.yticks(fontproperties='Times New Roman', size=12,weight='bold')plt.legend(loc='upper center',frameon=False)plt.show()
但还是不够顺眼,能不能同一天的只显示一个标签,而下面是它的时刻呢?
Matplotlib主副刻度与时间定位器
Matplotlib是允许设置主副刻度标签的,那么这个问题变开始变得很好解决,只要设置不同的主副刻度标签即可。
此外, Matplotlib还提供了不同的时间定位器(locator),我们将二者的结合,便可以打上需要的时间刻度标签:
plt.ylabel("upward longwave flux W/m^2",font1)plt.xlabel('Time',font1)plt.xlim(np.min(times), np.max(times))dates1=pd.date_range(np.min(times), np.max(times),freq='1D')dates2=pd.date_range(np.min(times), np.max(times),freq='6H')day = mdates.DayLocator(interval=1)ax.xaxis.set_major_locator(day) #设置主刻度ax.xaxis.set_major_formatter(mdates.DateFormatter('%m-%d')) #设置副刻度格式hoursLoc = mdates.HourLocator(interval=6) #为20小时为1副刻度ax.xaxis.set_minor_locator(hoursLoc)ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H'))#设置主刻度旋转角度和刻度label刻度间的距离padax.tick_params(which='major',axis='x',length=5,pad=10,direction='in')ax.tick_params(which='minor',axis='x',direction='in',labelsize=9)plt.xticks(fontproperties='Times New Roman', size=12,weight='bold')plt.yticks(fontproperties='Times New Roman', size=12,weight='bold')plt.legend(loc='upper center',frameon=False)plt.show()
完成,终于顺眼一点lorz
如果觉得《python Matplotlib中时间刻度标签的设置/主副刻度》对你有帮助,请点赞、收藏,并留下你的观点哦!