0

I have a txt file that contains a matrix with N rows and M columns. I would like to create some sort of surface graph where I can see N different curves each one plotting all the elements in that row.

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

The code I am currently using

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

But this is only producing a 2D plot.

I have also tried the following:

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)

However this gives an error

ValueError: shape mismatch: objects cannot be broadcast to a single shape
Manolete
  • 3,431
  • 7
  • 54
  • 92

1 Answers1

0

I got it working. The problem was in nx and 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)

enter image description here

Manolete
  • 3,431
  • 7
  • 54
  • 92