0

I have a text file containing 500x500 intensity values. I'm attempting to produce a 3D plot of this data using Python-XY. Only a particular part of this 500x500 array is meant to be plotted (235:268, 210:280).

Here is one of many attempts of the Python-XY code so far:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

data = np.genfromtxt('Fig2.txt')

X = np.linspace(235,268,num=33)
Y = np.linspace(210,280,num=70)
x,y = np.meshgrid (X,Y)
Z = data[235:268,210:280]

ax.plot_surface(x,y,Z, rstride=1, cstride=1, cmap=cm.jet)

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

I have attempted a number of different codes to produce this plot but there is a particular error that keeps popping up:

ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 0.

I'm assuming this is referring to the X and Y axis being incompatible but I'm unsure as to why this is. Is there anywhere in particular that I am going wrong here?

Azaxa
  • 49
  • 7

1 Answers1

1

In order to plot a bunch of points on 3 axes, each point needs to have an x, y and z value. So if you want to plot 10 points, for example, the 3 lists that you will pass to plot_surface (one each for the x, y and z axes) should have 10, 10 and 10 values in them. This provides an x, y and z value for all 10 points. Your x, y and Z lists do not have an equal number of elements. Some points have a y value, but no x value.

Before you call plot_surface, try inserting this line of code:

print len(x), len(y), len(Z)

Those 3 values need to be equal.

Jason
  • 2,725
  • 2
  • 14
  • 22
  • Thanks. It's led me onto discovering the issue. I believe there could be an issue with how the text file is written. Printing data[0,1:10] or any variation of this provides an array [nan, nan... nan]. The format for the text file is written as `[4.001e+01, 4.002e+01... 4.500e+01]`, `[4.501e+01, 4.502e+01... 5.000e+01]`, `... 500 lines` `[10.001e+01, 10.002e+01... 10.500e+01]` Could it be an issue with the fact there are no indents but are commas splitting each element or how "e+01" is included? – Azaxa Nov 07 '15 at 18:29