0

我有一个包含N行和M列矩阵的 txt 文件。我想创建某种曲面图,在其中我可以看到N不同的曲线,每条曲线都绘制了该行中的所有元素。

head output.txt

0.001194  0.001184  0.001499  0.002410  0.002337  0.002323  0.001685 0.01194
0.002260  0.002152  0.002390  0.001305  0.001270  0.001303  0.001155 0.01194
0.001232  0.002307  0.002127  0.001672  0.002278  0.002427  0.002136 0.01194
0.001950  0.001359  0.001137  0.001168  0.001208  0.001189  0.002564 0.01194   
0.002334  0.002345  0.002461  0.002223  0.002138  0.002352  0.001299 0.01194
0.001320  0.001184  0.001239  0.001466  0.002454  0.002349  0.002383 0.01194

我目前使用的代码

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('output.txt')

X = np.arange(len(data))
plt.plot(X, data)

plt.show()

但这只是产生二维图。

我还尝试了以下方法:

from mpl_toolkits.mplot3d import Axes3D

nx = len(data)
ny = len(data[0])
x = range(nx)
y = range(ny)

hf = plt.figure()
ha = hf.add_subplot(111, projection='3d')

X, Y = np.meshgrid(x, y)  
ha.plot_surface(X, Y, data)

然而,这给出了一个错误

ValueError: shape mismatch: objects cannot be broadcast to a single shape
4

1 回答 1

0

我让它工作了。问题出在nx和中ny

nx = len(data[0])
ny = len(data)

x = range(nx)
y = range(ny)

hf = plt.figure()
ha = hf.add_subplot(111, projection='3d')

X, Y = np.meshgrid(x, y)  
ha.plot_surface(X, Y, data)

在此处输入图像描述

于 2018-08-12T19:12:57.337 回答