美文网首页
Python 颜色渐变三维平滑曲线 3D plot Color

Python 颜色渐变三维平滑曲线 3D plot Color

作者: 音符纸飞机 | 来源:发表于2019-08-21 14:12 被阅读0次

    需求
    给定若干三维坐标点,用平滑曲线将他们连接起来,并且颜色与第三维的绝对值相关
    参考文档
    https://cloud.tencent.com/developer/ask/132612

    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from scipy.interpolate import interp1d
    import numpy as np
    
    # sample points
    x = [3, 6, 3, 6, 3, 1]
    y = [4, 7, 2, 1, 4, 3]
    z = [4, 9, 7, 5, 9, 6]
    
    # 首尾相连
    for i in (x, y, z):
        i.append(i[0])
    
    print(x)
    
    '''
    
    """
    A value in x_new is above the interpolation
    => the range of interp_i_x must within np.linspace(min(x), max(x), 7) 
    
    """
    
    # interp_i_x = np.linspace(min(x), max(x), 100)
    # interp_i_y = np.linspace(min(y), max(y), 100)
    # interp_i_z = np.linspace(min(z), max(z), 100)
    # xi = interp1d(np.linspace(min(x), max(x), 7), x, kind='quadratic')(interp_i_x)
    # yi = interp1d(np.linspace(min(y), max(y), 7), y, kind='quadratic')(interp_i_y)
    # zi = interp1d(np.linspace(min(z), max(z), 7), z, kind='quadratic')(interp_i_z)
    '''
    
    
    def interp_helper(values, num=50, kind='quadratic'):
        interp_i = np.linspace(min(values), max(values), num)
        return interp1d(np.linspace(min(values), max(values), len(values)), values, kind=kind)(interp_i)
    
    
    x_new, y_new, z_new = (interp_helper(i, 100) for i in (x, y, z))
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    zmax = np.array(z_new).max()
    zmin = np.array(z_new).min()
    
    for i in range(len(z_new) - 1):
        ax.plot(x_new[i:i + 2], y_new[i:i + 2], z_new[i:i + 2],
                color=plt.cm.jet(int((np.array(z_new[i:i + 2]).mean() - zmin) * 255 / (zmax - zmin))))
    plt.show()
    
    

    相关文章

      网友评论

          本文标题:Python 颜色渐变三维平滑曲线 3D plot Color

          本文链接:https://www.haomeiwen.com/subject/zebbsctx.html