簡體   English   中英

繪制兩個不同長度的不同陣列

[英]Plotting two different arrays of different lengths

我有兩個數組。 一個是長度(1000,)的原始信號,另一個是長度(100,)的平滑信號。 我想直觀地表示平滑信號如何表示原始信號。 由於這些數組的長度不同,我無法將它們繪制成另一個。 有沒有辦法在matplotlib中這樣做?

謝謝!

正如rth所建議的 ,定義

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

然后繪制原始對x1和平滑對x2:

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

np.linspace(0, 1, N)返回長度為N的數組, np.linspace(0, 1, N)等值為0到1(含)。


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()

產量 在此輸入圖像描述

這項工作需要兩個不同的x軸。 您無法在一個繪圖中繪制具有不同長度的兩個變量。

import matplotlib.pyplot as plt
import numpy as np

y = np.random.random(100) # the smooth signal
x = np.linspace(0,100,100) # it's x-axis

y1 = np.random.random(1000) # the raw signal
x1 = np.linspace(0,100,1000) # it's 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()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM