Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Matplotlib.dates.epoch2num() in Python

  • Last Updated : 19 Apr, 2020

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.


The matplotlib.dates.epoch2num() function is used to convert an epoch or a sequence of epochs to a new date format from the day since 0001.

Syntax: matplotlib.dates.epoch2num(e)


  • e: It can be an epoch or a sequence of epochs.

Returns: A new date format since day 0001.

Example 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,
               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()


Example 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):
figure = Figure(figsize =(5, 5), dpi = 100)
axes = figure.add_subplot(111)
line, = axes.plot([], [])
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().pack(side = BOTTOM, 
                            fill = BOTH, 
                            expand = True)
for cnt in range (600):
ani = animation.FuncAnimation(figure, animate, interval = 1000)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!