Constructing two different arrays of different lengths
I have two arrays. One is a raw length signal (1000) and the other is a smooth length signal (100). I want to visually represent how a smooth signal is a raw signal. Since these arrays are of different lengths, I cannot build them on top of each other. Is there a way to do this in matplotlib?
Thank!
+3
source to share
2 answers
As rth suggested , define
x1 = np.linspace(0, 1, 1000)
x2 = np.linspace(0, 1, 100)
and then draw 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 equidistant 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()
gives
+7
source to share
For this task you will need two different x-axes. You cannot construct two variables with different lengths in the same plot.
import matplotlib.pyplot as plt
import numpy as np
y = np.random.random(100) # the smooth signal
x = np.linspace(0,100,100) # it x-axis
y1 = np.random.random(1000) # the raw signal
x1 = np.linspace(0,100,1000) # it x-axis
fig = plt.figure()
ax = fig.add_subplot(121)
ax.plot(x,y,label='smooth-signal')
ax.legend(loc='best')
ax2 = fig.add_subplot(122)
ax2.plot(x1,y1,label='raw-signal')
ax2.legend(loc='best')
plt.suptitle('Smooth-vs-raw signal')
fig.show()
+1
source to share