plt绘制3D渐变填充柱状图

python 复制代码
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np

def make_bar(ax, x0=0, y0=0, width = 0.5, height=1 , cmap="viridis",
              norm=matplotlib.colors.Normalize(vmin=0, vmax=1), **kwargs ):
    # Make data
    u = np.linspace(0, 2*np.pi, 4+1)+np.pi/4.
    v_ = np.linspace(np.pi/4., 3./4*np.pi, 100)
    v = np.linspace(0, np.pi, len(v_)+2 )
    v[0] = 0 ;  v[-1] = np.pi; v[1:-1] = v_
    x = np.outer(np.cos(u), np.sin(v))
    y = np.outer(np.sin(u), np.sin(v))
    z = np.outer(np.ones(np.size(u)), np.cos(v))

    xthr = np.sin(np.pi/4.)**2 ;  zthr = np.sin(np.pi/4.)
    x[x > xthr] = xthr; x[x < -xthr] = -xthr
    y[y > xthr] = xthr; y[y < -xthr] = -xthr
    z[z > zthr] = zthr  ; z[z < -zthr] = -zthr

    x *= 1./xthr*width; y *= 1./xthr*width
    z += zthr
    z *= height/(2.*zthr)
    #translate
    x += x0; y += y0
    #plot
    ax.plot_surface(x, y, z, cmap=cmap, norm=norm, **kwargs)

def make_bars(ax, x, y, height, width=1):
    widths = np.array(width)*np.ones_like(x)
    x = np.array(x).flatten()
    y = np.array(y).flatten()

    h = np.array(height).flatten()
    w = np.array(widths).flatten()
    norm = matplotlib.colors.Normalize(vmin=0, vmax=h.max())
    for i in range(len(x.flatten())):
        make_bar(ax, x0=x[i], y0=y[i], width = w[i] , height=h[i], norm=norm)

if __name__ == '__main__':
    fig = plt.figure()
    ax = fig.add_subplot(111, projection=Axes3D.name)

    X, Y = np.meshgrid([1,2,3,4], [2,3,4])
    Z = np.array([[90.34,99.9,89.3,87.4],
                  [60.9,80.3,90,78.3],
                  [89.3,87.0,94.4,89.3]])

    make_bars(ax, X, Y, Z, width=0.2)
    plt.show()
相关推荐
再__努力1点32 分钟前
【59】3D尺度不变特征变换(SIFT3D):医学影像关键点检测的核心算法与实现
人工智能·python·算法·计算机视觉·3d
gihigo199833 分钟前
3D环境下的射线追踪实现
3d
谈笑也风生35 分钟前
3D可视化工具 | Lumion 12中文正式版下载教程指南
3d
码银39 分钟前
3D动态圣诞树代码
3d·html
科研面壁者21 小时前
SPSS——绘制“简单分布散点图”,“矩阵散点图”,“简单点图”,“重叠散点图”,“3D分布散点图”
3d·信息可视化·矩阵·数据分析·spss·科研绘图
laoliu19961 天前
使用VScode+AI大模型驱动Blender建模
3d·ai·blender
吃吃今天努力学习了吗1 天前
【论文阅读】Gaussian Grouping: Segment and Edit Anything in 3D Scenes
论文阅读·计算机视觉·3d·3dgs·三维分割
CG_MAGIC2 天前
用ZBrush和Blender制作波洛3D肖像
3d·blender·效果图渲染·zbrush·渲云渲染
Renderbus瑞云渲染农场2 天前
Maya相关插件有哪些?Maya云渲染插件哪些好用?
3d·云计算·3dsmax·maya
YANshangqian2 天前
Agisoft Metashape Pro(3D摄影建模软件)
3d