write_image (via kaleido) visually distorts large square heatmaps through aliasing artifacts
@ayjayt arbeitet bereits daran.
Seit 03.6.2025.
Bewertung
Dieses Issue wurde noch nicht bewertet.
Beschreibung
I have a relatively large matrix that I want to plot usinggraph_objects.Heatmap. The matrix has some structure that should become immediately apparent to the viewer (i.e. it has a constant diagonal and is symmetric). However due to visual artifacts, it looks like these properties are violated in the image produced by write_image (with the kaleido backend, haven't tested orca).
It was difficult to write a simple example script to reproduce these distortions, as variations in both the matrix and output image size can affect the issue significantly (both in intensity and quality). In any case it looks to be an aliasing artifact related to the selection of heatmap cell data that results in a non-bijective map between the heatmap cells and regions within the (vector) output image.
In the script below, one output format is PDF to rule out image compression as a culprit (as there the heatmap appears to be stored as a vector image as its cells remain sharp when zoomed in). However, the issue can be reproduced also when the output format is PNG. I assume that in the vector-output case, the heatmap data is (badly) rescaled like a bitmap image whose pixels are then re-interpreted as an array of square cells, even though those cells then do not match the heatmap/data cells any more.
#!/usr/bin/env python3
import numpy as np
import plotly.graph_objects as go
import plotly.io as pio
resultions = [
# Gives very unequal cell sizes, making the result look like a mosaic:
(200, 400, 400),
# Looks almost correct, but some diagonal elements show bad values:
(400, 400, 400),
]
for n, width, height in resultions:
# Produce a random matrix with key properties (symmetric, constant
# diagonal) matching my actual data.
np.random.seed(1)
A = np.random.random(size=(n, n))
A += A.T
np.fill_diagonal(A, -1)
# Plot the matrix as a square heatmap.
fig = go.Figure()
fig.add_trace(go.Heatmap(z=A))
fig.update_layout(
autosize=False,
width=width,
height=height,
margin=dict(b=0, l=0, r=0, t=0),
yaxis1=dict(scaleanchor="x1", constrain="domain"),
yaxis2=dict(scaleanchor="x2", constrain="domain"),
)
for ext in ("pdf", "png"):
pio.write_image(fig, f"n={n}_{width}x{height}.{ext}")
The outcomes are:
PDF:
n=200_400x400.pdf
n=400_400x400.pdf
PNG (note in the first image that only the heatmap, not the text looks blurred):


- For n = 200 and a layout size of 400x400, many adjacent rows and columns appear to use the same heatmap data. As a result the diagonal looks to be composed of alternating small and large blue cells. On closer inspection you can see that there are two locations where a double row and a double column meet off-diagonal, so that the result looks asymmetric.
- For n = 400 and a layout size of 400x400, the result looks mostly correct but some main diagonal cells are not blue, i.e. they have not been sampled from the data's main diagonal.
Given that my understanding of the problem is correct, I would suggest:
- For bitmap output, use a better interpolation method to scale the heatmap image to its output size.
- For vector output, if possible, do not go the middle route of rescaling a bitmap image. Instead rescale the vector data.
- Vorherrschende Sprache
- Python
- Sterne
- 504
- Forks
- 65
- Ø Merge
- 6 T. 9 Std.
- Gemergte PRs (30 T.)
- 2
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus plotly/kaleido
-
Writing to an existing directory whose name has a dot fails with "Invalid format"Evtl. vergeben @RaphaelFakhri hat das vor 14 Tagen übernommen. Offenbug P3 size: 1
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 35/100
-
Deterministic Rendering ChallengesEvtl. wieder frei @camdecoster hat das vor 69 Tagen übernommen, und es ist kein Pull Request offen. Offenenhancement feature P2 size: 5
-
[FEATURE]: Embed custom fonts into vector exports (no system install)Evtl. vergeben @camdecoster hat das vor 126 Tagen übernommen. Offenenhancement P3 size: 3
-
enhancement feature P3 size: 5
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 25/100
-
GlobalKaleidoServer is not thread-safeEvtl. vergeben @talleibman hat das vor 251 Tagen übernommen. Offen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
Ähnliche Issues
-
fuzz fuzz-unconfirmed
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 1 Tag
-
ready-for-agent
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
neilsh/stunt-reel#220 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Issue]: use_fp4_cache is accepted but ignored; the launcher always writes FP8 recordsEvtl. vergeben @0z5a hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
area: cli priority: p3 size: S type: bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 83/100
finos/open-resource-broker#757 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
Maintainer antworten meist innerhalb von 6 Tagen