Skip to content

CLN: Matplotlib imports in style #58921

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

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 67 additions & 83 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

from contextlib import contextmanager
import copy
from functools import partial
import operator
Expand Down Expand Up @@ -56,7 +55,6 @@

if TYPE_CHECKING:
from collections.abc import (
Generator,
Hashable,
Sequence,
)
Expand Down Expand Up @@ -84,22 +82,6 @@

from pandas import ExcelWriter

try:
import matplotlib as mpl
import matplotlib.pyplot as plt

has_mpl = True
except ImportError:
has_mpl = False


@contextmanager
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
if has_mpl:
yield plt, mpl
else:
raise ImportError(f"{func.__name__} requires matplotlib.")


####
# Shared Doc Strings
Expand Down Expand Up @@ -3832,61 +3814,61 @@ def _background_gradient(
else: # else validate gmap against the underlying data
gmap = _validate_apply_axis_arg(gmap, "gmap", float, data)

with _mpl(Styler.background_gradient) as (_, _matplotlib):
smin = np.nanmin(gmap) if vmin is None else vmin
smax = np.nanmax(gmap) if vmax is None else vmax
rng = smax - smin
# extend lower / upper bounds, compresses color range
norm = _matplotlib.colors.Normalize(smin - (rng * low), smax + (rng * high))
smin = np.nanmin(gmap) if vmin is None else vmin
smax = np.nanmax(gmap) if vmax is None else vmax
rng = smax - smin
_matplotlib = import_optional_dependency(
"matplotlib", extra="Styler.background_gradient requires matplotlib."
)
# extend lower / upper bounds, compresses color range
norm = _matplotlib.colors.Normalize(smin - (rng * low), smax + (rng * high))

if cmap is None:
rgbas = _matplotlib.colormaps[_matplotlib.rcParams["image.cmap"]](norm(gmap))
else:
rgbas = _matplotlib.colormaps.get_cmap(cmap)(norm(gmap))

def relative_luminance(rgba) -> float:
"""
Calculate relative luminance of a color.

The calculation adheres to the W3C standards
(https://www.w3.org/WAI/GL/wiki/Relative_luminance)

Parameters
----------
color : rgb or rgba tuple

Returns
-------
float
The relative luminance as a value from 0 to 1
"""
r, g, b = (
x / 12.92 if x <= 0.04045 else ((x + 0.055) / 1.055) ** 2.4
for x in rgba[:3]
)
return 0.2126 * r + 0.7152 * g + 0.0722 * b

if cmap is None:
rgbas = _matplotlib.colormaps[_matplotlib.rcParams["image.cmap"]](
norm(gmap)
def css(rgba, text_only) -> str:
if not text_only:
dark = relative_luminance(rgba) < text_color_threshold
text_color = "#f1f1f1" if dark else "#000000"
return (
f"background-color: {_matplotlib.colors.rgb2hex(rgba)};"
f"color: {text_color};"
)
else:
rgbas = _matplotlib.colormaps.get_cmap(cmap)(norm(gmap))

def relative_luminance(rgba) -> float:
"""
Calculate relative luminance of a color.

The calculation adheres to the W3C standards
(https://www.w3.org/WAI/GL/wiki/Relative_luminance)

Parameters
----------
color : rgb or rgba tuple

Returns
-------
float
The relative luminance as a value from 0 to 1
"""
r, g, b = (
x / 12.92 if x <= 0.04045 else ((x + 0.055) / 1.055) ** 2.4
for x in rgba[:3]
)
return 0.2126 * r + 0.7152 * g + 0.0722 * b

def css(rgba, text_only) -> str:
if not text_only:
dark = relative_luminance(rgba) < text_color_threshold
text_color = "#f1f1f1" if dark else "#000000"
return (
f"background-color: {_matplotlib.colors.rgb2hex(rgba)};"
f"color: {text_color};"
)
else:
return f"color: {_matplotlib.colors.rgb2hex(rgba)};"
return f"color: {_matplotlib.colors.rgb2hex(rgba)};"

if data.ndim == 1:
return [css(rgba, text_only) for rgba in rgbas]
else:
return DataFrame(
[[css(rgba, text_only) for rgba in row] for row in rgbas],
index=data.index,
columns=data.columns,
)
if data.ndim == 1:
return [css(rgba, text_only) for rgba in rgbas]
else:
return DataFrame(
[[css(rgba, text_only) for rgba in row] for row in rgbas],
index=data.index,
columns=data.columns,
)


def _highlight_between(
Expand Down Expand Up @@ -4124,20 +4106,22 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
rgbas = None
if cmap is not None:
# use the matplotlib colormap input
with _mpl(Styler.bar) as (_, _matplotlib):
cmap = (
_matplotlib.colormaps[cmap]
if isinstance(cmap, str)
else cmap # assumed to be a Colormap instance as documented
)
norm = _matplotlib.colors.Normalize(left, right)
rgbas = cmap(norm(values))
if data.ndim == 1:
rgbas = [_matplotlib.colors.rgb2hex(rgba) for rgba in rgbas]
else:
rgbas = [
[_matplotlib.colors.rgb2hex(rgba) for rgba in row] for row in rgbas
]
_matplotlib = import_optional_dependency(
"matplotlib", extra="Styler.bar requires matplotlib."
)
cmap = (
_matplotlib.colormaps[cmap]
if isinstance(cmap, str)
else cmap # assumed to be a Colormap instance as documented
)
norm = _matplotlib.colors.Normalize(left, right)
rgbas = cmap(norm(values))
if data.ndim == 1:
rgbas = [_matplotlib.colors.rgb2hex(rgba) for rgba in rgbas]
else:
rgbas = [
[_matplotlib.colors.rgb2hex(rgba) for rgba in row] for row in rgbas
]

assert isinstance(align, str) # mypy: should now be in [left, right, mid, zero]
if data.ndim == 1:
Expand Down