Python:用点值

时间:2016-05-27 13:57:50

标签: python matplotlib plot 3d mplot3d

在2D平面中,我有一组由nxn坐标定义的(x,y)点。对于每个点,存在一定数量我想要绘制为3D表面。我怎样才能以这样的方式进行,即在图中为每个点分配相关值?我问,因为我的情节混乱了。

我会更好地解释自己。我有:

  1. 飞机上10x10=100点的位置字典:
  2. dict1={0:(0, 0), 1:(0, 1), 2:(0, 2), ..., 99:(9,9)}

    1. 将与所述点相关联的值字典:
    2. dict2=OrderedDict([(0, 369670), (1, 370622), (2, 267034), ..., (99, 217500)])

      1. 合并dict1dict2,其中每个值与正确的点相关联,每个点都根据其坐标进行标记:
      2. merged_dict={dict1[k]: v for k, v in dict2.items()}

        merged_dict={(0,0):369670, (0,1):370622, (0,2):267034, ..., (9,9): 217500}

        预期的3D绘图的点坐标为X,Y和Z来自merged_dict。这是我的尝试:

        #3D plot
        import numpy as np
        from mpl_toolkits.mplot3d import Axes3D
        from matplotlib import cm
        
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        inds=[(0, 0), (0, 1), (0, 2), ..., (9,9)] #The coordinates of each point -> len(inds)=100
        
        X=[]
        for k in range(len(inds)):
            X.append(int(inds[k][0]))
        Y=X
        
        X, Y = np.meshgrid(X, Y)
        
        merged_dict = {dict1[k]: v for k, v in dict2.items()}
        
        Z = merged_dict.values()
        
        surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter, linewidth=0, antialiased=True)
        
        ax.set_zlim(0, 900000)
        
        ax.zaxis.set_major_locator(LinearLocator(10))
        ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
        
        plt.show()
        

        我得到的情节错误因为它有一个混乱的表面。预期的结果是对称的钟形表面,峰值超过800,000,对应于点(4,4),(5,4),(4,5),(5,5)。相反,结果让我认为Z值与相关的X,Y坐标没有正确关联。 如何解决此问题? enter image description here

        修改

        这些是涉及的实际数据:

        In[1]: merged_dict
        Out[1]: 
        {(0, 0): 369670,
         (0, 1): 370622,
         (0, 2): 267034,
         (0, 3): 169500,
         (0, 4): 116014,
         (0, 5): 116014,
         (0, 6): 169500,
         (0, 7): 267034,
         (0, 8): 370622,
         (0, 9): 369670,
         (1, 0): 370622,
         (1, 1): 491950,
         (1, 2): 456750,
         (1, 3): 370180,
         (1, 4): 308118,
         (1, 5): 308118,
         (1, 6): 370180,
         (1, 7): 456750,
         (1, 8): 491950,
         (1, 9): 370622,
         (2, 0): 267034,
         (2, 1): 456750,
         (2, 2): 542718,
         (2, 3): 554980,
         (2, 4): 543588,
         (2, 5): 543588,
         (2, 6): 554980,
         (2, 7): 542718,
         (2, 8): 456750,
         (2, 9): 267034,
         (3, 0): 169500,
         (3, 1): 370180,
         (3, 2): 554980,
         (3, 3): 689848,
         (3, 4): 759272,
         (3, 5): 759272,
         (3, 6): 689848,
         (3, 7): 554980,
         (3, 8): 370180,
         (3, 9): 169500,
         (4, 0): 116014,
         (4, 1): 308118,
         (4, 2): 543588,
         (4, 3): 759272,
         (4, 4): 888268,
         (4, 5): 888268,
         (4, 6): 759272,
         (4, 7): 543588,
         (4, 8): 308118,
         (4, 9): 116014,
         (5, 0): 116014,
         (5, 1): 308118,
         (5, 2): 543588,
         (5, 3): 759272,
         (5, 4): 888268,
         (5, 5): 888268,
         (5, 6): 759272,
         (5, 7): 543588,
         (5, 8): 308118,
         (5, 9): 116014,
         (6, 0): 169500,
         (6, 1): 370180,
         (6, 2): 554980,
         (6, 3): 689848,
         (6, 4): 759272,
         (6, 5): 759272,
         (6, 6): 689848,
         (6, 7): 554980,
         (6, 8): 370180,
         (6, 9): 169500,
         (7, 0): 267034,
         (7, 1): 456750,
         (7, 2): 542718,
         (7, 3): 554980,
         (7, 4): 543588,
         (7, 5): 543588,
         (7, 6): 554980,
         (7, 7): 542718,
         (7, 8): 456750,
         (7, 9): 267034,
         (8, 0): 370622,
         (8, 1): 491950,
         (8, 2): 456750,
         (8, 3): 370180,
         (8, 4): 308118,
         (8, 5): 308118,
         (8, 6): 370180,
         (8, 7): 456750,
         (8, 8): 491950,
         (8, 9): 370622,
         (9, 0): 369670,
         (9, 1): 370622,
         (9, 2): 267034,
         (9, 3): 169500,
         (9, 4): 116014,
         (9, 5): 116014,
         (9, 6): 169500,
         (9, 7): 267034,
         (9, 8): 370622,
         (9, 9): 369670}
        

1 个答案:

答案 0 :(得分:4)

我想行SELECT table1.ord_no, table2.item_no, table2.item_desc_1, table1.cus_no, table2.unit_price, table3.prc_or_disc_1, table2.line_seq_no FROM table2 JOIN table1 ON table1.ord_no = table2.ord_no LEFT OUTER JOIN table3 ON table2.item_no = table3.cd_tp_1_item_no AND table2.cus_no = table3.cd_tp_1_cust_no AND getdate() BETWEEN start_dt AND end_dt WHERE table2.ord_no = $multi_orders ORDER BY table2.line_seq_no 中存在问题。 Y=X是列表。当您撰写X, Y时,表示Y=X是对Y的引用。你需要复制,即:

X

测试这个变体并写下会发生什么。否则,将merged_dict的数据发布到真实值的测试图上。

<强>更新

看看数字,这是正确的吗? 数组Y = [] numpy.copy(Y,X) 必须是2d数组,但在您的代码中它是1d列表。

Z

enter image description here

更新2:

由于dict中的数据存储而出现问题,但dict未按索引排序。要将值加载到数组import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm fig = plt.figure() ax = fig.gca(projection='3d') X = np.arange(0,10,1) Y = np.arange(0,10,1) X, Y = np.meshgrid(X, Y) merged_dict = {(0, 0): 369670, (0, 1): 370622, (0, 2): 267034, ... (9, 8): 370622, (9, 9): 369670} Z = np.array(merged_dict.values()).reshape(10,10) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.winter, linewidth=0, antialiased=True) ax.set_zlim(0, 900000) plt.show()

Z

结果,你得到:

enter image description here