Plot numpy datetime64 with Matplotlib

MatplotlibPythonData Visualization

To plot a time series in Python using matplotlib, we can take the following steps −

  • Create x and y points using numpy.

  • Plot the created x and y points using plot() method.

  • To display the figure, use show() method.


import matplotlib.pyplot as plt
import datetime
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)])
y = np.random.randint(100, size=x.shape)
plt.plot(x, y)


Updated on 12-May-2021 12:20:25