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()
相关推荐
Zldaisy3d5 天前
中南大学 | 热稳定纳米析出相助力增材制造Al-Fe-Cr-Sc-Zr合金400℃长期热暴露后室温强度保持
3d·制造
陆康永5 天前
3d swipe 拖动 -改为linear css
3d·swiper
Zldaisy3d5 天前
增材制造陶瓷的强化策略:机制与工艺-结构-性能关系的多尺度综述
3d·制造
在下胡三汉6 天前
用 glTF/glb 加载自定义 3D 模型
3d·gltf
李航19838 天前
用 DeepDraw 几何引擎开发建筑设计软件(十):推挤工具
python·3d
iNeuOS工业互联网10 天前
大模型(DeepSeek)辅助 3D 实时建模 + 拖拽配置:业务可视化应用快速构建实践(标注、巡检与视角控制等)
人工智能·物联网·3d·语言模型·工业互联网
Zldaisy3d10 天前
点亮高端制造轻量化新图景:铝基材料增材制造进展与挑战
3d·制造
事界见闻10 天前
网页版AI 3D工具通常能完成哪些基础工作?生成、处理与导出流程对比
人工智能·3d
数字新视界10 天前
国产信创动环监控系统赋能智能环境安全新未来
物联网·3d·数字孪生·三维可视化·数据可视化
chaoyuanl11 天前
超元力主题乐园飞行影院:把景区文化做成可持续更新的沉浸体验
大数据·3d·xr·娱乐·mr