Plotting two different arrays of different lengths in matplotlib

MatplotlibPythonData Visualization

To plot two different arrays of different lengths in matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create y1, x1, y2 and x2 data points using numpy with different array lengths.
  • Plot x1, y1 and x2, y2 data points using plot() method.
  • To display the figure, use show() method.

Example

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

y1 = (np.random.random(100) - 0.5).cumsum()
y2 = y1.reshape(-1, 10).mean(axis=1)

x1 = np.linspace(0, 1, 100)
x2 = np.linspace(0, 1, 10)

plt.plot(x1, y1)
plt.plot(x2, y2)

plt.show()

Output

It will produce the following output

raja
Published on 18-Sep-2021 12:06:25
Advertisements