3D绘图中两条线之间的着色

时间:2016-09-10 17:20:43

标签: python matplotlib

我正在尝试填充3D线条之间的空间。

我有以下代码:

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()

一些合成数据:

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

产生这个:

enter image description here

现在我想填充这些行之间的空格并说出z=-30 NOT z=0这就是我想要改变的地方。

df.index.values0之间的值并说1000ang数据框的值范围为-3010

因此,我正在尝试制作一个偏移版本:

enter image description here

1 个答案:

答案 0 :(得分:1)

评论中我建议的另一个解决方案是使用fill_between;在那里你可以设置下边界。 fill_between会返回PolyCollection,因此您可以将其添加到类似于您现在所做的3d数字中:

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])

enter image description here