繁体   English   中英

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

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

我的 MATLAB 代码:

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

我试图在 python 中做同样的事情,但它不起作用。 为什么?

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

它显示此错误:

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

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

[padding,y_test]在 Python 和 MATLAB 中不做同样的事情。在 MATLAB 中,它沿着第一维连接两个 arrays。 在 Python 中,它创建了一个包含两个 arrays 作为其两个元素的列表。

要连接两个 NumPy arrays,请使用np.concatenatenp.stackcolumn_stack

在您的情况下,您想执行np.concatenate((padding, y_test)) ,假设paddingy_test是 1D arrays(如您的代码生成的那样)。 如果它们是形状为 1xN 的 2D arrays(如您在评论中所述),则指定您要沿二维连接: np.concatenate((padding, y_test), axis=1)

暂无
暂无

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

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