pistal
pistal

Reputation: 2456

Surface plot with different number of points in x, y and z axis

I'm trying to plot a surface plot with 549 points. The x axis has 51 points and y-axis has 9 points. and the z-axis has 549 points. For example:

fig  = plt.figure()
X = list(xrange(0,51))
Y = list(xrange(0,9))
Z = list(xrange(0,459))

print len(X)
print len(Y)
print len(Z)
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z, cmap=plt.cm.jet, cstride=1, rstride=1)
plt.savefig('graph-1' + '.jpg', bbox_inches='tight', pad_inches=0.2,dpi=100)
plt.clf()

And I try to plot it I get the following error:

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

How do we plot when we have different axis lengths?

The 3-tuple looks like this:

for a in range(0,len(X)):
 for b in range(0, len(Y)):
  for c in range(0, len(Z)):
    print (a,b,c)

Upvotes: 2

Views: 5479

Answers (2)

pistal
pistal

Reputation: 2456

Thanks to @Andrey

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random


def fun(x, y):
        return test[x][y]

global test
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = list(xrange(0,9))
y = list(xrange(0,51))
test = [[a for a in range(0, len(y)] for b in range(0, len(x))]
X, Y = np.meshgrid(x, y)
zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
ax.plot_surface(X, Y, Z)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

Upvotes: 1

Garth5689
Garth5689

Reputation: 622

Edit:

Ok, looking at this again, you want to plot 459 point, on a grid. Using this loop:

for a in range(0,len(X)):
    for b in range(0, len(Y)):
        for c in range(0, len(Z)):
            print (a,b,c)

will give you many more than 459 points, it will give you 51*9*459 points.

try this:

import itertools
X2,Y2=zip(*list(itertools.product(X,Y)))

This will create all possible combinations of x,y then you should be able to plot (X2,Y2,Z). len(X2) and len(Y2) are both 459

Upvotes: 0

Related Questions