Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting two different arrays of different lengths

I have two arrays. One is the raw signal of length (1000, ) and the other one is the smooth signal of length (100,). I want to visually represent how the smooth signal represents the raw signal. Since these arrays are of different length, I am not able to plot them one over the other. Is there a way to do so in matplotlib?

Thanks!

like image 713
piby180 Avatar asked Jun 23 '15 12:06

piby180


People also ask

How do you plot two arrays of different lengths in Python?

MatPlotLib with Python 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.

How do I plot two arrays in Matlab?

To produce a plot in MATLAB you need two arrays, one for the x-values and one for the y-values. These can come from a calculation or you can enter them by hand. For example, if x = [1 2 3 4] and y = [2 3 2 5] then you can plot these by plot(x,y).

How do you Multi plot?

In Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot.

Can you make multiple plots with MatPlotLib?

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.


1 Answers

As rth suggested, define

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

and then plot raw versus x1, and smooth versus x2:

plt.plot(x1, raw)
plt.plot(x2, smooth)

np.linspace(0, 1, N) returns an array of length N with equally spaced values from 0 to 1 (inclusive).


import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2015)

raw = (np.random.random(1000) - 0.5).cumsum()
smooth = raw.reshape(-1,10).mean(axis=1)

x1 = np.linspace(0, 1, 1000)
x2 = np.linspace(0, 1, 100)
plt.plot(x1, raw)
plt.plot(x2, smooth)
plt.show()

yields enter image description here

like image 134
unutbu Avatar answered Oct 28 '22 09:10

unutbu