Skip to content

[Bug]: Minor "squiggles" in spy output #32506

Closed as duplicate
Closed as duplicate

Description

@fedarko

Bug summary

Sometimes, the images produced by spy() contain visual artifacts that seem to make straight diagonal lines appear as if they contained "squiggles." This problem does not appear to be present in imshow() when called on the same exact input.

Code for reproduction

from io import StringIO
from scipy.io import mmread
from matplotlib import pyplot

# Load matrix data into memory. This corresponds to a genomic
# dot plot (k = 11) of two DNA sequences: https://github.com/fedarko/wotplot/issues/26

mt = """%%MatrixMarket matrix coordinate integer general
102 283 134
102 1 1
101 2 1
100 3 1
99 4 1
98 5 1
97 6 1
96 7 1
95 8 1
94 9 1
93 10 1
92 11 1
91 12 1
90 13 1
89 14 1
88 15 1
87 16 1
86 17 1
85 18 1
84 19 1
83 20 1
82 21 1
81 22 1
80 23 1
79 24 1
78 25 1
77 26 1
76 27 1
75 28 1
74 29 1
73 30 1
72 31 1
71 32 1
70 33 1
69 34 1
68 35 1
67 36 1
66 37 1
65 38 1
64 39 1
63 40 1
62 41 1
61 42 1
60 43 1
59 44 1
58 45 1
57 46 1
56 47 1
55 48 1
54 49 1
53 50 1
52 51 1
51 52 1
50 53 1
49 54 1
48 55 1
47 56 1
46 57 1
45 58 1
44 59 1
43 60 1
42 61 1
41 62 1
40 63 1
39 64 1
38 65 1
37 66 1
36 67 1
67 217 1
66 218 1
65 219 1
64 220 1
63 221 1
62 222 1
61 223 1
60 224 1
59 225 1
58 226 1
57 227 1
56 228 1
55 229 1
54 230 1
53 231 1
52 232 1
51 233 1
50 234 1
49 235 1
48 236 1
47 237 1
46 238 1
45 239 1
44 240 1
43 241 1
42 242 1
41 243 1
40 244 1
39 245 1
38 246 1
37 247 1
36 248 1
35 249 1
34 250 1
33 251 1
32 252 1
31 253 1
30 254 1
29 255 1
28 256 1
27 257 1
26 258 1
25 259 1
24 260 1
23 261 1
22 262 1
21 263 1
20 264 1
19 265 1
18 266 1
17 267 1
16 268 1
15 269 1
14 270 1
13 271 1
12 272 1
11 273 1
10 274 1
9 275 1
8 276 1
7 277 1
6 278 1
5 279 1
4 280 1
3 281 1
2 282 1
1 283 1
"""

# https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.mmread.html#mmread
m = mmread(StringIO(mt)).todense()

fig, axes = pyplot.subplots(3, 1)
ylabelparams = {"rotation": 0, "ha": "right"}

axes[0].spy(m, markersize=1)
axes[0].set_ylabel("spy (markersize = 1)", **ylabelparams)

axes[1].spy(m, markersize=0.5)
axes[1].set_ylabel("spy (markersize = 0.5)", **ylabelparams)

axes[2].imshow(m)
axes[2].set_ylabel("imshow", **ylabelparams)

fig.set_size_inches(10, 8)
pyplot.show()

Actual outcome

Here is the image produced by the above code. Note how the spy() plots for this figure appear to contain "squiggles" in their lines, rather than looking like perfect diagonal lines; and how, by comparison, the imshow() plot does not contain these squiggles.

This phenomenon does not appear to be due to visual occlusion (i.e. markers overlapping each other), since it occurs even when the marker size is set to 0.5.

Image

Expected outcome

Ideally, all of the spy() plots above would not contain these "squiggles."

Additional information

This isn't a huge deal, but it is somewhat strange. For datasets of this size I could just use imshow(), but since the matrices I am working with can be quite large I would prefer to use spy().

Operating system

Ubuntu 22.04.5 LTS

Matplotlib Version

3.11.2

Matplotlib Backend

tkagg

Python version

3.13.9

Jupyter version

7.5.0

Installation

conda

Activity

  1. jklymak commented on Oct 19, 2026

    @jklymak
    Member

    Try snap=False and/or increasing your resolution.

  2. fedarko commented on Oct 20, 2026

    @fedarko
    Author

    Thanks you! Setting snap=False for the spy() calls did not seem to fix this:

    Image

    However, I tried increasing the figure's DPI up from the default of 100, and this seemed to address the problem:

    DPI = 100 200 300 400 500
    Image Image Image Image Image

    (These figures do not have snap=False set.)

    I would prefer to leave the DPI as is if possible, but if this problem will always occur when using spy() at low resolutions then I can increase it.

  3. jklymak commented on Oct 20, 2026

    @jklymak
    Member

    If you don't have enough pixels to represent a marker, then it will be distorted. This is just a resolution issue. I do not think there is anything Matplotlib can do here.

  4. fedarko commented on Oct 20, 2026

    @fedarko
    Author

    Got it! I am a bit confused, however -- because as far as I can tell all three of these subplots should have enough pixels to show each of the markers (and imshow is able to do this without adjusting the DPI).

    For what it's worth, I used this code to set the size of the figure to 1000 pixels by 1000 pixels, and the issue persists: *

    ... # after loading the matrix "m" from above
    
    for dpi in [100]:
        px = 1/pyplot.rcParams['figure.dpi']  # pixel in inches
        fig, axes = pyplot.subplots(3, 1, figsize=(1000 * px, 1000 * px))
        ylabelparams = {"rotation": 0, "ha": "right"}
        
        axes[0].spy(m, markersize=1)
        axes[0].set_ylabel(f"spy (markersize = 1)\nFigure DPI = {dpi:,}", **ylabelparams)
        
        axes[1].spy(m, markersize=0.5)
        axes[1].set_ylabel("spy (markersize = 0.5)", **ylabelparams)
        
        axes[2].imshow(m)
        axes[2].set_ylabel("imshow", **ylabelparams)
    
        fig.savefig(f"dpi{dpi}-1k.png")
    Image

    * This figure is 10 inches by 10 inches, since the DPI is 100 and the figure is 1000 px by 1000 px. The top plot here uses a marker size of 1, and as I understand it marker sizes are given in points. Since there are 72 points per inch, this figure is 720 points by 720 points. I think this should be sufficiently large to show all markers within each of these ~100 x 300 images?

    Sorry if I am misunderstanding something! I just wanted to make sure this wasn't some sort of bug with matplotlib itself, since the fact that things are distorted so consistently seems strange.

  5. jklymak commented on Oct 20, 2026

    @jklymak
    Member

    Yes, scatter is meant to be fast, so it does an optimization to the nearest pixel: #7262. I was mistaken when I said snap=False would help, but increasing resolution definitely does.

    Having enough pixels to represent the data is not the same as those pixels lining up with your spy visualization. If you are super careful, you could make the pixels and your matrix pixels line up precisely, but unless you do that there will be "wiggles". The way to make the wiggles imperceptible. Note how all the markers line up exactly with pixels:

    Image

    Your image is not showing the same apparent problem because it is antialiased. If you zoom in you see many pixels that are not pure yellow or pure purple. That tricks your mind into thinking the "squares" are properly centred but they are not. Note how each yellow square is different:

    Image

    I'm going to close as a duplicate of #7262

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