Python matplot3d - 在同一个3D图上绘制两组数据

时间:2016-08-02 10:34:58

标签: python matplotlib plot

我试图在单个3D图上绘制2组数据。 我期望看到的是同一情节中的这两个图像: enter image description here

我正在寻找的情节是:enter image description here

我使用Anaconda和Jupiter,matplotlib 1.5.1。

到目前为止,这是我的代码:

from mpl_toolkits.mplot3d import Axes3D
import mpl_toolkits.mplot3d as a3
import matplotlib.pyplot as plt
import cop

class var1:
    def __init__(self,b1, b2):
        self.pt1 = copy.deepcopy(b1)       
        self.pt2 = copy.deepcopy(b2) 
        self.cords = (
            list(self.pt1 + [high]), # top-left
            list(self.pt2 + [high]), # top-right
            list(self.pt2 + [low]), # bottom-right 
            list(self.pt1 + [low]), # bottom-left
            )            
    def drawFunction(self):       
        dataSet1 = a3.art3d.Poly3DCollection([self.cords])
        dataSet1.set_color('firebrick')
        dataSet1.set_edgecolor('k')
        ax.add_collection3d(dataSet1)    #If you comment out this line- var2 will be shown. I`m trying to show one on top of the other
        var2(self.pt1, self.pt2, high, low).drawFunction()

class var2:
    def __init__(self,b1, b2, high, low):
        self.pt1 = copy.deepcopy(b1)       
        self.pt2 = copy.deepcopy(b2) 
        self.cords = (
            list(self.pt1 + [(high/2) + (high/4)]), # top-left
            list(self.pt2 +  [(high/2) + (high/4)]), # top-right
            list(self.pt2 +  [(high/2) - (high/4)]), # bottom-right 
            list(self.pt1 +  [(high/2) - (high/4)]), # bottom-left
            )       
    def drawFunction(self):
        dataSet2 = a3.art3d.Poly3DCollection([self.cords])
        dataSet2.set_color('cornflowerblue')
        dataSet2.set_edgecolor('k')
        ax.add_collection3d(dataSet2)

high = 500
low = 0
fig = plt.figure()
ax = Axes3D(fig)

i = 0 
while i<4:
    if (i==0):
        p1 = [0,200]
        p2 = [0,0]
    if (i==1):
        p1 = [0,0]
        p2 = [200,0]
    if (i==2):
        p1 = [200,0]
        p2 = [200,200]
    if (i==3):
        p1 = [200,200]
        p2 = [0,200]

    var1(p1, p2).drawFunction()
    i = i+1

ax.set_xlim(0,200)
ax.set_ylim(0, 200)
ax.set_zlim(0, 1000)
plt.show()

1 个答案:

答案 0 :(得分:0)

请注意,在示例代码中,两个图都存在,您可以通过以下方式Poly3DCollection([self.cords],alpha=0.5)更改代码来查看它。只是add_collection3d将一个吸引到另一个上,所以你没有看到它们。

我找不到打败它的方法,但您可以查看here以获得可能的解决方法。

顺便说一句,代码缺少import copyax = a3.Axes3D(fig)才能正常工作。