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()
相关推荐
美摄科技7 小时前
什么是3D贴纸SDK?
3d
HelloRevit21 小时前
快速入门 - Azure 数字孪生的 3D 场景工作室(预览版)入门
3d·flask·azure
CHOTEST中图仪器2 天前
3d光学轮廓仪如何局部测量标准台阶?
3d·光学轮廓仪·三维形貌·微观尺寸
前端_Danny2 天前
使用 ECharts + ECharts-GL 生成 3D 环形图
3d·信息可视化·echarts
学無芷境2 天前
Large-Scale 3D Medical Image Pre-training with Geometric Context Priors
人工智能·3d
暴风鱼划水3 天前
三维重建【4-A】3D Gaussian Splatting:代码解读
python·深度学习·3d·3dgs
老黄编程3 天前
pcl 3DSC特征描述符、对应关系可视化以及ICP配准
3d·pcl·3dsc·icp
猿来是你_L3 天前
UGUI笔记——3D坐标转换成UGUI坐标
笔记·3d
渲吧云渲染4 天前
3D 技术赋能制造企业精准高效装配生产
3d·数字化装配·制造业转型
不会学习的小白O^O5 天前
基于色彩融合和3D功能的自动苹果识别
3d