Menachem Kalmenson
Menachem Kalmenson

Reputation: 61

3D Plotting region fulfilling inequalities in matplotlib

Looking to shade the region in 3D space fulfilling the set of inequalities:

x^2 < -2*x*y*z

y^2 < -2*x*y*z

With z running between [0,1].

This can be done in Mathematica with the code

RegionPlot3D[
x^2 < -2*x*y*z && y^2 <- 2*x*y*z,  {x, -1, 1}, {y, -1, 1}, {z, 0, 1}, 
PlotPoints -> 100] 

I want to know how to do this in matplotlib.

3D plot

Upvotes: 4

Views: 2641

Answers (1)

mtmrv
mtmrv

Reputation: 55

I found this question searching an answer for just same question!

Since no one answered yet, I can propose you to go with Plotly for 3D or just with MatPlotLib for 2D and make what I call "integral trick": just go over the axes and put a scatter plot point at the coordinates that satisfy the inequality.

I propose Plotly for 3D because if you plot many points in MatPlotLib that it works really slow when you rotate the plot.

For your plot we can write the following:

import plotly.express as px
import numpy

data_x = []
data_y = []
data_z = []
for z in numpy.arange(0, 1, 0.01):
    for x in numpy.arange(-1, 1, 0.01):
        for y in numpy.arange(-1, 1, 0.01):
            if x**2 < -2*x*y*z and y**2 <- 2*x*y*z:
                data_x.append(x)
                data_y.append(y)
                data_z.append(z)


fig = px.scatter_3d(x=data_x, y=data_y, z=data_z, opacity=0.3)
fig.show()

Though it's just an approximate solid, it nevertheless helps.

Techincally, though, it's not a simple thing to do since you need to describe a mesh in terms of a region. It turns out that you need at least to find the corners for the solid.. So, Mathematica has a great function for this but to make the same in Python it seems we need to get hands dirty.

Upvotes: 1

Related Questions