python x轴显示指定时间刻度
生活随笔
收集整理的這篇文章主要介紹了
python x轴显示指定时间刻度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將x軸設置為時間軸
注意事項:
在對x軸進行時間轉換時需要注意:
- datetime.date2num():是從1,1,1開始計數,單位是天
- time.mktime():是從1970.1.1開始計數,單位是秒
實例
# 摘取自ww3項目Figure_area_average.py import time, datetime, calendar import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as dt import matplotlib.ticker as ticker from matplotlib.dates import DateFormatter, MONDAY, MonthLocator, YearLocator from Package_Read.Filter_tools import * # 畫圖 fig = plt.figure(figsize=[9, 5]) ax = fig.add_subplot(1, 1, 1)formatter = DateFormatter('%Y/%m/%d') time1 = datetime.date(1970, 1, 1) time2 = dt.date2num(time1) timex0=timex timex = timex/24/3600 + time2 ax.plot_date(timex[2:-1], dhf[2:-1], '-',label='high', alpha=1) ax.plot_date(timex[2:-1], dbf[2:-1], '-',label='band', alpha=1) ax.plot_date(timex[2:-1], dlf[2:-1], '-',label='low', alpha=1) # ax.set_alpha = 0.8 ax.xaxis.set_major_formatter(formatter) ax.yaxis.grid(True) # 顯示x刻度網格# 設置x軸時間刻度 yeardays=YearLocator() # 獲取每年第一日數據 mondays=MonthLocator() # 獲取每月第一日數據 locate=MonthLocator(range(1, 13), bymonthday=1, interval=6) # 獲取每3個月第一日數據ax.xaxis.set_major_locator(locate) # 設定主刻度 ax.xaxis.set_minor_locator(mondays) # 設定次刻度 # autodates = dt.AutoDateLocator() # 自動顯示x軸時間刻度 # ax.xaxis.set_major_locator(autodates) # 設置時間間隔 ax.xaxis.set_major_formatter(formatter) # 設置時間顯示格式 fig.autofmt_xdate() # 自動旋轉xlabel # ax.yaxis.set_ticks_position('both') # 在圖像左右兩側顯示y軸刻度 formatter_y=ticker.PercentFormatter(xmax=100) ax.yaxis.set_major_formatter(formatter_y) ax.legend(loc='upper right', shadow=False) # ax.set_xlabel('Time (year/month/day)') ax.set_ylabel('$\int(U_{air}-U_{oc})/U_{oc}dt$') ax.set_xlim(timex[0], timex[-1]) ax.set_title(f'Waves effect on surface current\n 'f'lon:{lon1}-{lon2} lat:{lat1}-{lat2}')fig.show() fig.savefig(fig_file,dpi=300)總結
以上是生活随笔為你收集整理的python x轴显示指定时间刻度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uboot 的i2c读写,i2c_rea
- 下一篇: linpack测试软件,标准Linpac