Astrid - 11 months ago 51

Python Question

I am trying the fill the space between my lines in 3D.

I have the following code:

`from mpl_toolkits.mplot3d import Axes3D`

from matplotlib.collections import PolyCollection

import matplotlib.pyplot as plt

import numpy as np

class plotting3D(object):

"""

Class to plot 3d

"""

def __init__(self):

pass

def cc(self, arg):

return colorConverter.to_rgba(arg, alpha=0.6)

def poly3d(self, df):

"""

Method to create depth of joints plot for GP regression.

"""

fig = plt.figure(figsize=(10,10))

ax = fig.gca(projection='3d')

which_joints = df.columns

dix = df.index.values

zs = [1,4]

verts = []

for j in which_joints:

verts.append(list(zip(dix,df[j])))

poly = PolyCollection(verts,facecolors=[self.cc('r'), self.cc('g')])

poly.set_alpha(0.6)

ax.add_collection3d(poly, zs=zs, zdir='y')

ax.set_ylim([0, 5])

ax.set_zlim([0, 20])

ax.set_xlim([0,dix[-1]])

ax.grid(False)

ax.set_xlabel('X')

ax.set_ylabel('Y')

ax.set_zlabel('Z')

plt.show()

Some synthetic data:

`k= pd.DataFrame(20*np.random.rand(10,2),columns=['foot','other_foot'])`

Produces this:

Now I want to fill the space between the lines and say

`z=-30`

`z=0`

`df.index.values`

`0`

`1000`

`ang`

`-30`

`10`

Hence, I am trying to produce an offset version of this:

Answer Source

Another solution to my suggestion in the comments is to use `fill_between`

; there you have the possibility to set the lower boundary. `fill_between`

returns a `PolyCollection`

, so you can add it to the `3d`

figure similar to what you are doing now:

```
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10,10))
ax = fig.gca(projection='3d')
# +/- your data:
z = [0,10,10,-20,10,0]
x = [0,1000,1500,2500,3000,3500]
ax.add_collection3d(plt.fill_between(x,z,0), zs=1, zdir='y') # lower boundary z=0
ax.add_collection3d(plt.fill_between(x,z,-30), zs=5, zdir='y') # lower boundary z=-30
ax.set_ylim([0, 5])
ax.set_zlim([-30, 20])
ax.set_xlim([0,3500])
```