Matplotlib是Python中令人惊叹的可视化库,用于二维阵列图。 Matplotlib是一个基于NumPy数组构建的multi-platform数据可视化库,旨在与更广泛的SciPy堆栈配合使用。
matplotlib.dates.epoch2num()
的matplotlib.dates.epoch2num()
函数用于从0001开始的日期开始将一个时期或一系列时期转换为新的日期格式。
用法: matplotlib.dates.epoch2num(e)
参数:
- e:它可以是一个纪元或一个纪元序列。
返回值:自日期0001开始的新日期格式。
范例1:
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# generate some random data
# for approx 5 yrs
random_data = [float(random.randint(1487517521,
14213254713))
for _ in range(1000)]
# convert the epoch format to
# matplotlib date format
mpl_data = mdates.epoch2num(random_data)
# plotting the graph
fig, axes = plt.subplots(1, 1)
axes.hist(mpl_data, bins = 51, color ='green')
locator = mdates.AutoDateLocator()
axes.xaxis.set_major_locator(locator)
axes.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
plt.show()
输出:
范例2:
from tkinter import *
from tkinter import ttk
import time
import matplotlib
import queue
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
import matplotlib.dates as mdate
root = Tk()
graphXData = queue.Queue()
graphYData = queue.Queue()
def animate(objData):
line.set_data(list(graphXData.queue),
list(graphYData.queue))
axes.relim()
axes.autoscale_view()
figure = Figure(figsize =(5, 5), dpi = 100)
axes = figure.add_subplot(111)
axes.xaxis_date()
line, = axes.plot([], [])
axes.xaxis.set_major_formatter(mdate.DateFormatter('%H:%M'))
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack(side = BOTTOM,
fill = BOTH,
expand = True)
for cnt in range (600):
graphXData.put(matplotlib.dates.epoch2num(time.time()-(600-cnt)))
graphYData.put(0)
ani = animation.FuncAnimation(figure, animate, interval = 1000)
root.mainloop()
输出:
注:本文由纯净天空筛选整理自RajuKumar19大神的英文原创作品 Matplotlib.dates.epoch2num() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。