has2k1 / has2k1/plotnine

All categorical values are displayed in the legend when source dataframe is polars.DataFrame

Open
#977 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

critical upstream-bug
Dominant language
Python
Stars
4.8k
Forks
254
Avg merge
10h 35m
Merged PRs (30d)
9

Description

This is the example that produces all categorical values in the legend, which is unexpected. I copied it from a notebook, so might need tweaking to display elsewhere.


import polars as pl
import polars.selectors as cs
import datetime as dt
import random

import plotnine
from plotnine import *

import mizani
from mizani.formatters import log_format, custom_format, date_format, label_number, label_percent, label_bytes
from mizani.breaks import date_breaks

# turn on String Cache, otherwise we can't concat different dataframes into one
pl.enable_string_cache()


date = dt.datetime(2025, 8, 29)          # any date is fine
start = dt.datetime(date.year, date.month, date.day, 6, 0, 0)
end   = dt.datetime(date.year, date.month, date.day, 18, 0, 0)

# timestamps: every 5 minutes, inclusive of both ends (06:00 and 18:00)
timestamps = pl.datetime_range(
    start=start,
    end=end,
    interval="5m",
    closed="both",
    eager=True
)

random.seed(42)

sources = pl.Series("source", ["u", "w", "v"], dtype=pl.Categorical)
dirs = pl.Series("dir", ["incoming", "outgoing"], dtype=pl.Categorical)

df = (
    pl.DataFrame({"source": sources})
    .join(
        pl.DataFrame({"five_min_slot": timestamps}),
        how="cross"
    )
    .join(
        pl.DataFrame({"dir": dirs}),
        how="cross"
    )
    .with_columns(
        values=pl.lit(100.0),
    )
    .with_columns(
        pl.col('values').map_elements(lambda v: v*random.random(), return_dtype=pl.self_dtype()),
    )
)

(
    ggplot(
        df.filter(
            pl.col('source').eq('u')
        )
        .sort('five_min_slot'),
        aes(x='five_min_slot', y='values', color='factor(dir)')
    )
    + geom_line()
    + theme_linedraw()
    + theme(
        figure_size=(16, 5),
        axis_text_x=element_text(rotation=45, ha='right'),
    )
    + scale_x_datetime(
        labels=date_format("%H:%M"),
        date_breaks="15 minute",
        date_minor_breaks="5 minute",
        expand=(0, 0),
    )
    + scale_y_continuous(
        labels=label_percent(scale=1),
    )

)

For that code, I get the following output:

Image

I would expect the legend to contain only incoming and outgoing.

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 example with the Polars dataframe and compare the generated legend with the expected incoming and outgoing entries. Trace how categorical values from the filtered dataframe are collected for the legend, then verify that unused values are excluded and the example produces only those two entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.