Skip to content

patches.Scatter() and patches.Plot() should be added. #29318

Closed
@hyperkai

Description

@hyperkai

Problem

Only using patches.Rectangle() of plt.subplots() works properly as shown below:

from torchvision.datasets import CelebA

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)
    rect = Rectangle(xy=(x, y), width=w, height=h, linewidth=3, edgecolor='r', facecolor='none', clip_on=True) # Here
    axis.add_patch(p=rect)                                                                                     # Here

Screenshot 2024-12-16 012242

And only using axes.Axes.scatter() of plt.subplots() works properly as shown below:

from torchvision.datasets import CelebA
import torchvision

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)

    for px, py in lm.split(2):                # Here
        axis.scatter(x=px, y=py, c='#1f77b4') # Here

Screenshot 2024-12-16 020807

And only using axes.Axes.plot() of plt.subplots() works properly as shown below:

from torchvision.datasets import CelebA
import torchvision

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)

    px = []                    # Here
    py = []                    # Here
    for j, v in enumerate(lm): # Here
        if j%2 == 0:           # Here
            px.append(v)       # Here
        else:                  # Here
            py.append(v)       # Here
    axis.plot(px, py)          # Here

Screenshot 2024-12-16 021239

But using patches.Rectangle() of plt.subplots() with axes.Axes.scatter() of plt.subplots() doesn't work properly as shown below:

from torchvision.datasets import CelebA

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)
    rect = Rectangle(xy=(x, y), width=w, height=h, linewidth=3, edgecolor='r', facecolor='none', clip_on=True) # Here
    axis.add_patch(p=rect)                                                                                     # Here

    for px, py in lm.split(2):                # Here
        axis.scatter(x=px, y=py, c='#1f77b4') # Here

Screenshot 2024-12-16 012506

And using patches.Rectangle() of plt.subplots() with axes.Axes.plot() of plt.subplots() doesn't work properly as shown below:

from torchvision.datasets import CelebA

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)
    rect = Rectangle(xy=(x, y), width=w, height=h, linewidth=3, edgecolor='r', facecolor='none', clip_on=True) # Here
    axis.add_patch(p=rect)                                                                                     # Here

    px = []                    # Here
    py = []                    # Here
    for j, v in enumerate(lm): # Here
        if j%2 == 0:           # Here
            px.append(v)       # Here
        else:                  # Here
            py.append(v)       # Here
    axis.plot(px, py)          # Here

Screenshot 2024-12-16 013942

So instead, I used patches.Rectangle() of plt.subplots() with patches.Circle() of plt.subplots(), then they properly work as shown below:

from torchvision.datasets import CelebA

my_data = CelebA(
    root="data",
    split="all",
    target_type=["bbox", "landmarks"]
)

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle

fig, axes = plt.subplots(nrows=2, ncols=5, figsize=(12, 6))
for (im, ((x, y, w, h), lm)), axis in zip(my_data, axes.ravel()):
    axis.imshow(X=im)
    rect = Rectangle(xy=(x, y), width=w, height=h, linewidth=3, edgecolor='r', facecolor='none', clip_on=True) # Here
    axis.add_patch(p=rect)                                                                                     # Here

    for px, py in lm.split(2):                # Here
        axis.add_patch(p=Circle(xy=(px, py))) # Here

Screenshot 2024-12-16 014626

Proposed solution

So, it seems like matplotlib.patches works with matplotlib.patches so patches.Scatter() and patches.Plot() should be added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions