plot 2 time series into a single graph matlab in python

Here is how you can plot 2 time series into a single graph in Python using matplotlib library:

main.py
import matplotlib.pyplot as plt
import numpy as np

# Generate some sample data
x = np.arange("2018-01-01", "2018-01-05", dtype="datetime64[h]")
y1 = np.random.randint(1, 10, size=len(x))
y2 = np.random.randint(1, 10, size=len(x))

# plot the data
plt.plot(x, y1, label="Time Series 1")
plt.plot(x, y2, label="Time Series 2")

# Add labels and legend
plt.xlabel("Date/Time")
plt.ylabel("Values")
plt.title("Two Time Series")
plt.legend()

# Show the plot
plt.show()
466 chars
21 lines

This will plot y1 and y2 as two time series from x starting from 2018-01-01 and ending at 2018-01-04. plt.plot function is used to plot both the series on a single graph. plt.xlabel, plt.ylabel, and plt.title functions are used to add labels to X and Y axis and the graph title. plt.legend() function adds a legend to the plot to distinguish the two time series from each other. Finally, plt.show() function displays the plot on the screen.

related categories

gistlibby LogSnag