gpt4 book ai didi

matplotlib - 在 Matplotlib 中向 3D 集合添加矩形补丁和文本补丁

转载 作者:行者123 更新时间:2023-12-04 07:55:05 25 4
gpt4 key购买 nike

问题陈述
我正在尝试将两个补丁——一个矩形补丁和一个文本补丁——添加到 3D 图中的同一空间。最终目标是用相应的值(跨越 4 个平面的大约 20 个矩形——见图 3)来注释矩形块。下面的代码并没有完全到达那里,但确实演示了一个渲染问题,有时文本补丁是完全可见的,有时则不是——有趣的是,如果字符串没有延伸到矩形补丁之外,它似乎永远不会出现变得可见。图 1 和图 2 之间的唯一区别是绘图查看器图像的旋转。我在下面的示例中保留了 cmap 代码,因为它是项目的要求(以防万一它影响结果)。
我尝试过的事情

  • 颠倒绘制补丁的顺序。
  • 应用 zorder 值——我认为 art3d.pathpatch_2d_to_3d压倒一切。
  • 创建补丁集合——我似乎找不到将矩形补丁和文本补丁添加到同一个 3D 集合的方法。

  • 结论
    我怀疑在将每个补丁添加到 3D 集合之前为其设置 zorder 可能是解决方案,但我似乎无法找到达到该结果的方法。类似的问题表明了这一点,但我无法将他们的答案专门应用于此问题。
    环境
    macOS:大苏尔 11.2.3
    python 3.8
    Matplotlib 3.3.4
    图1
    Text patch partially visible
    图2
    Text patch fully visible
    图 3
    Actual patches to annotate
    编码
    生成图 1 和图 2(不是图 3)。
    #! /usr/bin/env python3
    # -*- coding: utf-8 -*-

    from matplotlib.patches import Rectangle, PathPatch
    from matplotlib.text import TextPath
    from matplotlib.transforms import Affine2D
    import mpl_toolkits.mplot3d.art3d as art3d
    import matplotlib.pyplot as plt
    from matplotlib.colors import Normalize


    plt.style.use('dark_background')
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    cmap = plt.cm.bwr
    norm = Normalize(vmin=50, vmax=80)
    base_color = cmap(norm(50))

    # Draw box
    box = Rectangle((25, 25), width=50, height=50, color=cmap(norm(62)), ec='black', alpha=1)
    ax.add_patch(box)
    art3d.pathpatch_2d_to_3d(box, z=1, zdir="z")

    # Draw text
    text_path = TextPath((60, 50), "xxxx", size=10)
    trans = Affine2D().rotate(0).translate(0, 1)
    p1 = PathPatch(trans.transform_path(text_path))
    ax.add_patch(p1)
    art3d.pathpatch_2d_to_3d(p1, z=1, zdir="z")

    ax.set_xlabel('x')
    ax.set_xlim(0, 100)
    ax.set_xticklabels([])
    ax.xaxis.set_pane_color(base_color)

    ax.set_ylabel('y')
    ax.set_ylim(0, 100)
    ax.set_yticklabels([])
    ax.yaxis.set_pane_color(base_color)

    ax.set_zlabel('z')
    ax.set_zlim(1, 4)
    ax.set_zticks([1, 2, 3, 4])
    ax.zaxis.set_pane_color(base_color)
    ax.set_zticklabels([])

    plt.show()

    最佳答案

    这是 matplotlib 3D 绘图的一个众所周知的问题:对象按特定顺序绘制,最后绘制的对象出现在其他对象的“顶部”,无论哪个应该在“真实”3D 绘图的前面。
    请参阅此处的常见问题解答:https://matplotlib.org/mpl_toolkits/mplot3d/faq.html#my-3d-plot-doesn-t-look-right-at-certain-viewing-angles

    My 3D plot doesn’t look right at certain viewing angles

    This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look “physically correct.”

    Unfortunately, while some work is being done to reduce the occurrence of this artifact, it is currently an intractable problem, and can not be fully solved until matplotlib supports 3D graphics rendering at its core.

    The problem occurs due to the reduction of 3D data down to 2D + z-order scalar. A single value represents the 3rd dimension for all parts of 3D objects in a collection. Therefore, when the bounding boxes of two collections intersect, it becomes possible for this artifact to occur. Furthermore, the intersection of two 3D objects (such as polygons or patches) can not be rendered properly in matplotlib’s 2D rendering engine.

    This problem will likely not be solved until OpenGL support is added to all of the backends (patches are greatly welcomed). Until then, if you need complex 3D scenes, we recommend using MayaVi.

    关于matplotlib - 在 Matplotlib 中向 3D 集合添加矩形补丁和文本补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66738036/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com