plotly / plotly/Kaleido

rasterizing the heatmap layer into a tiny (blurry) bitmap when writing PDF

Open
#426 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
498
Forks
62
Avg merge
1d 11h
Merged PRs (30d)
2

Description

I noticed when we (cc @janosh) try to save the plotly generated heatmap with kaleido, the embedded heatmap in PDF became a tiny (and thus blurry) bitmap:

data.csv

repro.pdf

plotly=6.5.0
kaleido=1.2.0

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# /// script
# dependencies = ["pandas", "numpy", "plotly", "kaleido"]
# ///


from __future__ import annotations

from pathlib import Path
from importlib.metadata import version


import numpy as np
import pandas as pd
import plotly.graph_objects as go


print(f"plotly={version('plotly')}")
print(f"kaleido={version('kaleido')}")


def load_csv(csv_path: Path) -> pd.DataFrame:
    df = pd.read_csv(csv_path, index_col=0)
    df.index = pd.to_datetime(df.index.astype(str), format="%Y-%m", errors="raise")
    df.index.name = "time"
    return df


def prepare_heatmap_data(df_git: pd.DataFrame) -> tuple[pd.DataFrame, pd.DataFrame]:
    df_binned = df_git.resample("6ME").sum().rename_axis("time_binned")

    heatmap_data = df_binned.T
    heatmap_data.columns = heatmap_data.columns.to_series().dt.strftime("%Y-%m")

    log_data = np.log10(heatmap_data.replace(0, np.nan))
    return heatmap_data, log_data


def main() -> None:
    df_git = load_csv(Path("data.csv"))
    heatmap_data, log_data = prepare_heatmap_data(df_git)

    fig = go.Figure()
    fig.add_heatmap(
        z=log_data.to_numpy(),
        x=log_data.columns.to_list(),
        y=log_data.index,
        customdata=heatmap_data.to_numpy(),
    )
    fig.write_image("repro.pdf")


if __name__ == "__main__":
    main()
> pdfimages -list repro.pdf
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image      29    13  icc     3   8  image  no         5  0     6     4 1035B  92%
   1     1 smask      29    13  gray    1   8  image  no         5  0     6     4   48B  13%

Viewing that same PDF in macos preview is blurry, but on macos Chrome seems fine:
Image

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python reproduction with plotly 6.5.0 and kaleido 1.2.0, then inspect repro.pdf with pdfimages as shown. Trace the PDF export path for the heatmap and verify that the generated PDF no longer embeds the heatmap as a 29×13 bitmap or appears blurry in macOS Preview.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
66/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.