write_image (via kaleido) visually distorts large square heatmaps through aliasing artifacts
@ayjayt y travaille déjà.
Depuis le 3/6/2025.
Évaluation
Cette issue n'a pas encore été évaluée.
Description
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.
- Langage dominant
- Python
- Étoiles
- 504
- Forks
- 65
- Merge moyen
- 6 j 9 h
- PR mergées (30 j)
- 2
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de plotly/kaleido
-
Writing to an existing directory whose name has a dot fails with "Invalid format"Peut-être pris @RaphaelFakhri l’a pris il y a 15 jours. Ouvertebug P3 size: 1
Difficulté 2/5 1-3 heures Accessibilité débutants 35/100
-
Deterministic Rendering ChallengesPeut-être à nouveau libre @camdecoster l’a pris il y a 70 jours, et aucune pull request n’est ouverte. Ouverteenhancement feature P2 size: 5
-
[FEATURE]: Embed custom fonts into vector exports (no system install)Peut-être pris @camdecoster l’a pris il y a 127 jours. Ouverteenhancement P3 size: 3
plotly/kaleido#464 · 10 commentaires · 1 personne assignée ·
-
enhancement feature P3 size: 5
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 25/100
-
GlobalKaleidoServer is not thread-safePeut-être pris @talleibman l’a pris il y a 252 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 48/100
Toutes les issues de plotly/kaleido
Issues similaires
-
Remove the `pull_request_target` workflow before GitHub blocks itPeut-être pris @jat255 l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
IBM/mcp-context-forge#7483 ·
Les mainteneurs répondent en général sous 2 jours
-
bug good first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
[Bug Report]: OpenAI 辅助 API 连通性测试因输出上限仅 1 token 误报“未知错误”Peut-être pris @fjfanjian l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
Project-N-E-K-O/N.E.K.O#3640 ·
Les mainteneurs répondent en général sous 1 jour
-
author-page correction
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
acl-org/acl-anthology#10808 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour