简体   繁体   English

如何把plot下面的Matlab编码变成python?

[英]How to plot the following Matlab code into python?

My MATLAB code:我的 MATLAB 代码:

x=1:28:9996;
#y_test is 1x178 double array
padding=nan(1,179);
plot(x,[padding,y_test])

I am trying to do the same in python but it is not working.我试图在 python 中做同样的事情,但它不起作用。 why?为什么?

#python
x=np.arange(1,9996,28)
padding=np.full((179),np.nan)
plt.plot(x,[padding,y_test])

It show this error:它显示此错误:

ValueError: x and y must have same first dimension, but have shapes (1, 357) and (2,)

while the shape is y_test.shape, padding.shape,x.shape=>((1, 178), (1, 179), (1, 357)) thank you!而形状是y_test.shape, padding.shape,x.shape=>((1, 178), (1, 179), (1, 357))谢谢!

[padding,y_test] does not do the same thing in Python and in MATLAB. In MATLAB it concatenates the two arrays along the 1st dimension. [padding,y_test]在 Python 和 MATLAB 中不做同样的事情。在 MATLAB 中,它沿着第一维连接两个 arrays。 In Python it creates a list containing the two arrays as its two elements.在 Python 中,它创建了一个包含两个 arrays 作为其两个元素的列表。

To concatenate two NumPy arrays, use np.concatenate , np.stack , or column_stack .要连接两个 NumPy arrays,请使用np.concatenatenp.stackcolumn_stack

In your case, you want to do np.concatenate((padding, y_test)) , assuming padding and y_test are 1D arrays (as your code generates).在您的情况下,您想执行np.concatenate((padding, y_test)) ,假设paddingy_test是 1D arrays(如您的代码生成的那样)。 If they are 2D arrays with shape 1xN (as you claim in a comment), then specify you want to concatenate along the 2nd dimension: np.concatenate((padding, y_test), axis=1) .如果它们是形状为 1xN 的 2D arrays(如您在评论中所述),则指定您要沿二维连接: np.concatenate((padding, y_test), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM