SoFunction
Updated on 2024-10-30

3D graph visualization using python

This is the knowledge encountered in learning the tensorflow framework, here the definition of the function is chosen to encapsulate x and y to facilitate tensorflow derivation.

You'll have to get used to it.

import numpy as np
import  as plt
from mpl_toolkits.mplot3d import Axes3D
def himmelblau(x):
return (x[0]**2 + x[1] - 11)**2 + (x[0] + x[1]**2 - 7)**2
x = (-6, 6, 200)
y = (-6, 6, 200)
X, Y = (x, y)
fig = (figsize=(12, 10))
ax = (projection='3d')
ax.plot_surface(X, Y, Z)   # Drawing surfaces
# (X, Y, Z) # Draw curves as if x, y had to be one-dimensional.
ax.view_init(60, -30)       # It's like adjusting the angle of the chart
ax.set_xlabel('X')
ax.set_ylabel('Y')
()

Supplementary: python3 install mayavi, realize 3d dynamic visualization operation

The following code is found on the Internet, search keywords "python 3d dynamic visualization", from finding the code to the actual implementation of the success of the problem and the solution to record.

Environment win8 64 bit, python3.6

import numpy as np
from mayavi import mlab 
x, y = [-2:2:160j, -2:2:160j]
z = abs(x) * (-x ** 2 - (y / .75) ** 2)
pl = (x, y, z, warp_scale=2)
(xlabel='x', ylabel='y', zlabel='z')
(pl)
()

Question:

Although some articles have pointed out that the installation order when installing mayavi is PyQt4-->Traits-->VTK-->Mayavi, I don't know the reason why, so I still installed mayavi directly, and the following error message appeared:

Microsoft Visual C++ 14.0 is required

Reason:

Missing dependency packages

Solution:

According to the article, look in /~gohlke/pythonlibs/ to find the relevant dependencies for mayavi.

1、VTK through pip install VTK installation

2. PyQt4 can not be installed by pip install PyQt4, so you need to download to the Python installation directory under the Script (environment for python3.6, 64-bit Win), through the command pip install PyQt4-4.11.4-cp36-cp36m-win_amd64.whl:

3, Traits is not as mayavi dependency package, but belongs to a part of the ETS, the same by downloading the file, pip install traits-4.6.0-cp36-cp36m-win_amd64.whl installation

4, mayavi installation, the same by downloading the file, pip install mayavi-4.6.0+vtk81-cp36-cp36m-win_amd64.whl installation

Note: PyQt4, Traits, mayavi is not impossible to download and install directly from the Internet through pip install, should still be missing the relevant dependencies, here only to ensure that the installation through the file without error.

Question:

Error when running the code: ImportError: failed to import

Reason:

Since the packages related to mayavi are installed in the latest version, you need to update numpy.

Solution:

pip install --upgrade numpy

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to advise me.