Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically adjust HTML table text color when using `style.background_gradient()` #21258

Closed
joelostblom opened this issue May 30, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@joelostblom
Copy link
Contributor

commented May 30, 2018

Code Sample

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame(np.random.randn(8, 4), columns=list('ABCD'))
df.style.background_gradient()

image

Problem description

Since all text in the displayed HTML table is black, it is difficult to see values when the background is dark. This is currently possible to adjust manually with the high and low parameters, but this limits the range of the colormap.

A possible improvement would be to color text white automatically if the background color is too dark (low luminance). This approach is currently implemented for annotated heatmaps in the visualization library seaborn, and similar criteria could be used with pandas styles (luminance implementation and w3c's official definition).

Automatic text coloring could be exposed through a text_color parameter to style.background_gradient(), which could be either auto, or a color value. Another alternative is to let the user adjust the luminance threshold directly, with a sound default value. A third approach would be to create a new method for controlling the text color in general, and call it with specific arguments inside style.background_gradient().

If you think adding this functionality would be useful, I am happy to attempt a PR.

Expected Output

luminance threshold 0.408 (seaborn default)
screenshot-20180530-112153

luminance threshold 0.2
screenshot-20180530-112028

Output of pd.show_versions()

INSTALLED VERSIONS

commit: c85ab08
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.16.8-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.0.dev+2049.gc85ab0839.dirty
pytest: 3.5.1
pip: 9.0.3
setuptools: 39.2.0
Cython: 0.28.2
numpy: 1.14.3
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.4
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.7
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@WillAyd

This comment has been minimized.

Copy link
Member

commented May 30, 2018

I think it's reasonable - if you want to try your hand at a PR then please do!

joelostblom added a commit to joelostblom/pandas that referenced this issue May 30, 2018

joelostblom added a commit to joelostblom/pandas that referenced this issue May 30, 2018

joelostblom added a commit to joelostblom/pandas that referenced this issue May 30, 2018

TomAugspurger added a commit that referenced this issue Jun 7, 2018

ENH: Color text based on background in Styler (#21263)
* Color text based on background gradient

Closes #21258

david-liu-brattle-1 added a commit to david-liu-brattle-1/pandas that referenced this issue Jun 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.