plotnine icon indicating copy to clipboard operation
plotnine copied to clipboard

Automatically determine label color based on background color

Open lucascherzer opened this issue 2 months ago • 2 comments

In plots with inline text (labels), the text must be legible at all times, this entails changing the font color based on the background color.

The current workaround also advertised in the docs is to insert a new column into the dataframe that directly maps the color based on some threshold.

Since plotnine is responsible for mapping background colors, it could compute the font color for labels ad-hoc based on the background color such that it is always legible.

This would make the library more ergonomic for me. Currently, I spend more time than I would like to admit manually adjusting the threshold.

lucascherzer avatar Dec 01 '25 13:12 lucascherzer

There is a function in this example called overlay_color (probably a bad name) that computes the a contrasting threshold and chooses between two colors. I plan to include a version of it in the next release 0.16.0 as part of some "toolbox" or something, but you will have to use it explicitly.

has2k1 avatar Dec 02 '25 10:12 has2k1

okay, thanks for the quick feedback. Looking forward to its release :D

lucascherzer avatar Dec 02 '25 11:12 lucascherzer