How to plot a 3D weight function in python with matplotlib

给你一囗甜甜゛ 提交于 2021-02-11 14:47:51

问题


Is there any Python library/function in order to print in 3D the function "weight"

weight(x,y,z) = x**(y*z) + y**(x*z) + z**(x*y)

with Python and Matplotlib ?

Here is already an interesting link How to plot a 3D density map in python with matplotlib but it shows a density instead of a weight for distributed coordinates X Y Z, for example from -5 to +5 with regular steps 0.1 (where solutions for the weight exists in real numbers).

I am just playing around with some math functions and wanted to show this for teaching purpose, no immediate use identified.

Update 1: achievement so far with plotly

"""
release May22 2020
https://plotly.com/python/3d-volume-plots/
"""
import plotly.graph_objects as go
import numpy as np

#X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j]
X, Y, Z = np.mgrid[ 0.01:2:40j, 0.01:2:40j, 0.01:2:40j]
#
# pascal dagsi surfaces
#vol=(X**(Y*Z)) + (Y**(X*Z)) + (Z**(X*Y))
#vol=(X**(Y+Z)) + (Y**(X+Z)) + (Z**(X+Y))
#vol=(X**(Y+Z)) * (Y**(X+Z)) * (Z**(X+Y))
vol=(X**(Y*Z)) * (Y**(X*Z)) * (Z**(X*Y))

fig = go.Figure(data=go.Volume(
    x=X.flatten(),
    y=Y.flatten(),
    z=Z.flatten(),
    value=vol.flatten(),
    opacity=0.1,
    isomin=0.02,
    isomax=100,
    surface_count=22,
    caps=dict(x_show=False, y_show=False, z_show=False)
    ))
#
fig.show()

Update 2: other variant (iso surface)

"""
release May22 2020
https://plotly.com/python/3d-isosurface-plots/
"""
import plotly.graph_objects as go
import numpy as np
#X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j]
#X, Y, Z = np.mgrid[ 0.1:5:40j, 0.1:5:40j, 0.1:5:40j]
X, Y, Z = np.mgrid[ 0.01:2:40j, 0.01:2:40j, 0.01:2:40j]
#
# pascal dagsi surfaces
#values=(X**(Y*Z)) + (Y**(X*Z)) + (Z**(X*Y))
#values=(X**(Y+Z)) + (Y**(X+Z)) + (Z**(X+Y))
#values=(X**(Y+Z)) * (Y**(X+Z)) * (Z**(X+Y))
values=(X**(Y*Z)) * (Y**(X*Z)) * (Z**(X*Y))

fig = go.Figure(data=go.Isosurface(
    x=X.flatten(),
    y=Y.flatten(),
    z=Z.flatten(),
    value=values.flatten(),
    opacity=0.2,
    isomin=0.5,
    isomax=25,
    surface_count=10,
    caps=dict(x_show=False, y_show=False)
    ))
#
fig.show()

Update 3: mayavi works

"""
release May22 2020
pascal dagsi surfaces
"""
import numpy as np
from mayavi import mlab
#
x, y, z = np.ogrid[ 0.01:2:40j, 0.01:2:40j, 0.01:2:40j]
#
#s=(x**(y*z)) + (y**(x*z)) + (z**(x*y))
#s=(x**(y+z)) + (y**(x+z)) + (z**(x+y))
#s=(x**(y+z)) * (y**(x+z)) * (z**(x+y))
s=(x**(y*z)) * (y**(x*z)) * (z**(x*y))

src = mlab.pipeline.scalar_field(s)

mlab.pipeline.iso_surface(src, contours=[0.1, 0.2, 0.5, 1, 11, 1001, 100001], opacity=0.2,colormap='magma',vmin=0.01,vmax=0.99)

mlab.axes()
mlab.show()

来源:https://stackoverflow.com/questions/61806693/how-to-plot-a-3d-weight-function-in-python-with-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!