简体   繁体   中英

Arranging data to make a 3d surface plot

I am trying to make 3D surface plot, but I'm having trouble transforming my data into the right format.

Reshaping my Z variable gives me: 'ValueError: total size of new array must be unchanged'. The shape for Z is (2500,50). The shapes for X and Y are both (50,50). I'm not sure why this is so. Is there something obviously wrong with my code that I'm missing?

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


def Mountain(F, P):
    TA = ((TAmax-TAmin) * (((FF**g)/((FF**g)+FFhm**g)**a)/((((FF**g)/((FF**g)+FFhm**g))**a)+(SP/SPe)**a)))
    return TA


F = np.linspace(0,120)
P = np.linspace(0,100)
Fb= int(21)
Fro= int(362)
FFhm = int(63)
g= int(3)
a = int(3)
SPmin = float(1.82)
SPbend = float(0.5)
SPe = int(10)
TAmax= int(1)
TAmin = int(0)

SP = SPmin + SPbend * np.log(1 + np.exp((P - SPmin)/SPbend))

FF= (Fb * (np.log(1 + np.exp(Fro/Fb)) - np.log(1 + np.exp((Fro-F)/Fb))))


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(F, P)
gh= list(zip(np.ravel(X), np.ravel(Y)))
zs = np.array(Mountain(F,P) for F,P in gh)
Z = zs.reshape(X.shape)

ax.plot_surface(X, Y, Z)

ax.set_xlabel('F')
ax.set_ylabel('P')
ax.set_zlabel('TA')

plt.show()

Note that zs is empty. If you want to do list comprehension, you should use brackets (so np.array([Mountain(F,P) for F,P in gh]) ), though what you really want is zs = np.array([Mountain(X[i],Y[i]) for i in range(X.shape[0])]) instead of ravel.

绘图输出

You have to put z value for every pair of (X,Y). It mean you do not need numpy.ravel . The sizes of X, Y and Z must be the same:

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


def Mountain(F, P):
    TA = ((TAmax-TAmin) * (((FF**g)/((FF**g)+FFhm**g)**a)/((((FF**g)/((FF**g)+FFhm**g))**a)+(SP/SPe)**a)))
    return TA


F = np.linspace(0,120)
P = np.linspace(0,100)
Fb= int(21)
Fro= int(362)
FFhm = int(63)
g= int(3)
a = int(3)
SPmin = float(1.82)
SPbend = float(0.5)
SPe = int(10)
TAmax= int(1)
TAmin = int(0)

SP = SPmin + SPbend * np.log(1 + np.exp((P - SPmin)/SPbend))
FF= (Fb * (np.log(1 + np.exp(Fro/Fb)) - np.log(1 + np.exp((Fro-F)/Fb))))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(F, P)
Z = np.array(Mountain(F,P))

ax.plot_surface(X, Y, Z)

ax.set_xlabel('F')
ax.set_ylabel('P')
ax.set_zlabel('TA')

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM