使用来自文本文件的数据在python中进行3D绘图的问题

时间:2022-09-10 21:28:19

I have been trying to import a text file and plot it in python using "wireframe", although I keep getting an error saying:

我一直试图导入一个文本文件并使用“线框”在python中绘制它,虽然我一直收到错误说:

"ValueError: shape mismatch: objects cannot be broadcast to a single shape".

“ValueError:形状不匹配:对象无法广播到单个形状”。

I would appreciate it if anyone could help me with this. my input text file format is like:

如果有人能帮助我,我将不胜感激。我的输入文本文件格式如下:

11 12 13 14

11 12 13 14

21 22 23 24

21 22 23 24

31 32 33 34

31 32 33 34

and the code is:

而代码是:

`import os
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('X1slackresultsforplot.txt', delimiter=' ')

x = [0.78,0.79,0.8]
y = [10,20,30,40]
Z = np.array(data)
X, Y = np.meshgrid(x, y)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,Z,rstride=10,cstride=10)
plt.show()`

Thanks.

谢谢。

1 个解决方案

#1


1  

X and Y have shape (4, 3), but Z has shape (3, 4). Perhaps you want the transpose of Z? The following runs without an error:

X和Y具有形状(4,3),但Z具有形状(3,4)。也许你想要Z的转置?以下运行没有错误:

ax.plot_wireframe(X,Y,Z.T,rstride=10,cstride=10)

#1


1  

X and Y have shape (4, 3), but Z has shape (3, 4). Perhaps you want the transpose of Z? The following runs without an error:

X和Y具有形状(4,3),但Z具有形状(3,4)。也许你想要Z的转置?以下运行没有错误:

ax.plot_wireframe(X,Y,Z.T,rstride=10,cstride=10)