Skip to content

[Bug]: withTickedStroke raises ValueError when the line contains NaN #32689

Backlog

Description

@LegalZhang

Bug summary

The TickedStroke path effect (patheffects.withTickedStroke()) crashes with ValueError: cannot convert float NaN to integer when the line data contains NaN. Without the path effect, NaN simply breaks the line into pieces. The same happens with masked values (they become NaN inside Line2D), and with inf, which raises OverflowError: cannot convert float infinity to integer.

This is easy to hit when marking a constraint boundary computed from a formula that is undefined over part of the x range, for example a circle drawn as sqrt(r**2 - x**2).

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patheffects

x = np.linspace(-3, 3, 301)
with np.errstate(invalid="ignore"):
    upper = np.sqrt(4 - x**2)  # boundary of x**2 + y**2 <= 4; nan for |x| > 2

fig, ax = plt.subplots()
ax.plot(x, upper, path_effects=[patheffects.withTickedStroke(angle=-135)])
ax.plot(x, 0.5 * x - 1, path_effects=[patheffects.withTickedStroke()])
ax.set_aspect("equal")
fig.savefig("constraints.png")


Shortest version:


import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patheffects

fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3], [0, 1, np.nan, 3], path_effects=[patheffects.withTickedStroke()])
fig.canvas.draw()

Actual outcome

  File ".../matplotlib/patheffects.py", line 465, in draw_path
    num = int(np.ceil(s_total / spacing_px)) - 1
          ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot convert float NaN to integer

Expected outcome

The line is drawn in pieces, as without the path effect, with ticks along each piece.

Additional information

TickedStroke.draw_path measures the length of the polygons from Path.to_polygons(closed_only=False). For a path without codes (which is what Line2D produces), to_polygons returns the vertices as they are, NaN included, so the total length is NaN. Removing non-finite points first (Path.cleaned(remove_nans=True)) splits the path the same way the line itself is split. I can open a PR with that.

Same error with 3.11.2.

Operating system

macOS 26.6.2 (arm64)

Matplotlib Version

3.11.2 (release) and 3.12.0.dev981+g6b0c60d49 (main, commit 6b0c60d49)

Matplotlib Backend

agg

Python version

3.12.16

Jupyter version

No response

Installation

git checkout

Activity

  1. RjyavardhanSingh commented on Nov 29, 2026

    @RjyavardhanSingh

    Hi, I'd like to work on this. Could you assign it to me? I'll open a PR soon.

  2. lucaznch commented on Nov 30, 2026

    @lucaznch
    Contributor

    Hi @RjyavardhanSingh ,thanks for your interest in contributing. We do not assign issues but you're welcome to submit a PR. Please see our contributor's guide
    https://matplotlib.org/devdocs/devel/index.html

    And AI policy
    https://matplotlib.org/devdocs/devel/contribute.html#generative-ai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions