2

I know the best way is trying function Z = f(X, Y) and then using X,Y,Z,but my actual data is stored in a list,here I just give a simple example.

I want take i, j and test_list as X,Y,Z coordinate:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D 
test_list = []

for i in np.arange(1, 21):
    for j in range(1, 11):
        test_list.append(i * j)

so at first the shape of test_list is (200,) and then I reshape it as (20,10) and using meshgrid:

i = np.array(np.arange(1, 21))
j = np.array(np.arange(1, 11))

X, Y = np.meshgrid(i, j)

Z1  =  np.reshape(test_list, (20, 10))

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

ax.plot_surface(X, Y, Z1, rstride=1, cstride=1,
                cmap='viridis', edgecolor='none')

plt.show()

However it shows error:

shape mismatch: objects cannot be broadcast to a single shape

How to solve it, I guess there is no problem...

1 Answer 1

2

You're almost there. You create Z1 as shape (20, 10), however meshgrid creates X and Y as (10, 20). The solution is to transpose the array:

ax.plot_surface(X, Y, Z1.T, rstride=1, cstride=1,
                cmap='viridis', edgecolor='none')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.