matplotlib / matplotlib

matplotlib: plotting with Python
https://matplotlib.org/stable/
20.04k stars 7.59k forks source link

[Bug]: `patch_collection_2d_to_3d` fails to project 2d patches. #24352

Open JesseFarebro opened 1 year ago

JesseFarebro commented 1 year ago

Bug summary

patch_collection_2d_to_3d doesn't seem to work as expected. It seems it fails to project the patches correctly.

Code for reproduction

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.art3d as art3d
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle

plt, axs = plt.subplots(nrows=1, ncols=2, subplot_kw={"projection": "3d"})
for ax in axs:
    ax.set_xlim(0, 1.5)
    ax.set_ylim(0, 1.5)

# Works properly.
patches = [
    Rectangle((0, 0), 0.5, 0.5, facecolor="k"),
    Rectangle((0, 1), 0.5, 0.5, facecolor="k"),
    Rectangle((1, 1), 0.5, 0.5, facecolor="k"),
    Rectangle((1, 0), 0.5, 0.5, facecolor="k"),
]
for patch in patches:
    axs[0].add_patch(patch)
    art3d.pathpatch_2d_to_3d(patch, z=0, zdir="z")

# Does not work properly.
patches = PatchCollection(
    [
        Rectangle((0, 0), 0.5, 0.5, facecolor="k"),
        Rectangle((0, 1), 0.5, 0.5, facecolor="k"),
        Rectangle((1, 1), 0.5, 0.5, facecolor="k"),
        Rectangle((1, 0), 0.5, 0.5, facecolor="k"),
    ]
)
axs[1].add_collection(patches)
art3d.patch_collection_2d_to_3d(patches, zs=0, zdir="z")

plt.savefig("test.png")

Actual outcome

The first column is the expected output. The second column is attempting to use patch_collection_2d_to_3d.

test

Expected outcome

The first column in the image above is the expected outcome.

Additional information

No response

Operating system

No response

Matplotlib Version

3.6.0

Matplotlib Backend

MacOSX

Python version

3.10.6

Jupyter version

No response

Installation

pip

oscargus commented 1 year ago

Just for completeness: the same incorrect behavior happens when creating a Patch3DCollection directly, so probably the error is related more to Patch3DCollection than patch_collection_2d_to_3d.

(Interestingly, the only test for Patch3DCollection is a figure equality test. However, the resulting figure is not what one would expect.)