@@ -1470,6 +1470,13 @@ def hide_columns(self, subset: Subset) -> Styler:
1470
1470
# A collection of "builtin" styles
1471
1471
# -----------------------------------------------------------------------
1472
1472
1473
+ @doc (
1474
+ name = "background" ,
1475
+ alt = "text" ,
1476
+ image_prefix = "bg" ,
1477
+ axis = "{0 or 'index', 1 or 'columns', None}" ,
1478
+ text_threshold = "" ,
1479
+ )
1473
1480
def background_gradient (
1474
1481
self ,
1475
1482
cmap = "PuBu" ,
@@ -1483,9 +1490,9 @@ def background_gradient(
1483
1490
gmap : Sequence | None = None ,
1484
1491
) -> Styler :
1485
1492
"""
1486
- Color the background in a gradient style.
1493
+ Color the {name} in a gradient style.
1487
1494
1488
- The background color is determined according
1495
+ The {name} color is determined according
1489
1496
to the data in each column, row or frame, or by a given
1490
1497
gradient map. Requires matplotlib.
1491
1498
@@ -1501,7 +1508,7 @@ def background_gradient(
1501
1508
Compress the color range at the high end. This is a multiple of the data
1502
1509
range to extend above the maximum; good values usually in [0, 1],
1503
1510
defaults to 0.
1504
- axis : {0 or 'index', 1 or 'columns', None }, default 0
1511
+ axis : {axis }, default 0
1505
1512
Apply to each column (``axis=0`` or ``'index'``), to each row
1506
1513
(``axis=1`` or ``'columns'``), or to the entire DataFrame at once
1507
1514
with ``axis=None``.
@@ -1510,6 +1517,7 @@ def background_gradient(
1510
1517
or single key, to `DataFrame.loc[:, <subset>]` where the columns are
1511
1518
prioritised, to limit ``data`` to *before* applying the function.
1512
1519
text_color_threshold : float or int
1520
+ {text_threshold}
1513
1521
Luminance threshold for determining text color in [0, 1]. Facilitates text
1514
1522
visibility across varying background colors. All text is dark if 0, and
1515
1523
light if 1, defaults to 0.408.
@@ -1529,7 +1537,7 @@ def background_gradient(
1529
1537
.. versionadded:: 1.0.0
1530
1538
1531
1539
gmap : array-like, optional
1532
- Gradient map for determining the background colors. If not supplied
1540
+ Gradient map for determining the {name} colors. If not supplied
1533
1541
will use the underlying data from rows, columns or frame. If given as an
1534
1542
ndarray or list-like must be an identical shape to the underlying data
1535
1543
considering ``axis`` and ``subset``. If given as DataFrame or Series must
@@ -1543,6 +1551,10 @@ def background_gradient(
1543
1551
-------
1544
1552
self : Styler
1545
1553
1554
+ See Also
1555
+ --------
1556
+ Styler.{alt}_gradient: Color the {alt} in a gradient style.
1557
+
1546
1558
Notes
1547
1559
-----
1548
1560
When using ``low`` and ``high`` the range
@@ -1560,52 +1572,50 @@ def background_gradient(
1560
1572
1561
1573
Examples
1562
1574
--------
1563
- >>> df = pd.DataFrame({
1564
- ... 'City': ['Stockholm', 'Oslo', 'Copenhagen'],
1565
- ... 'Temp (c)': [21.6, 22.4, 24.5],
1566
- ... 'Rain (mm)': [5.0, 13.3, 0.0],
1567
- ... 'Wind (m/s)': [3.2, 3.1, 6.7]
1568
- ... })
1575
+ >>> df = pd.DataFrame(columns=["City", "Temp (c)", "Rain (mm)", "Wind (m/s)"],
1576
+ ... data=[["Stockholm", 21.6, 5.0, 3.2],
1577
+ ... ["Oslo", 22.4, 13.3, 3.1],
1578
+ ... ["Copenhagen", 24.5, 0.0, 6.7]])
1569
1579
1570
1580
Shading the values column-wise, with ``axis=0``, preselecting numeric columns
1571
1581
1572
- >>> df.style.background_gradient (axis=0)
1582
+ >>> df.style.{name}_gradient (axis=0)
1573
1583
1574
- .. figure:: ../../_static/style/bg_ax0 .png
1584
+ .. figure:: ../../_static/style/{image_prefix}_ax0 .png
1575
1585
1576
1586
Shading all values collectively using ``axis=None``
1577
1587
1578
- >>> df.style.background_gradient (axis=None)
1588
+ >>> df.style.{name}_gradient (axis=None)
1579
1589
1580
- .. figure:: ../../_static/style/bg_axNone .png
1590
+ .. figure:: ../../_static/style/{image_prefix}_axNone .png
1581
1591
1582
1592
Compress the color map from the both ``low`` and ``high`` ends
1583
1593
1584
- >>> df.style.background_gradient (axis=None, low=0.75, high=1.0)
1594
+ >>> df.style.{name}_gradient (axis=None, low=0.75, high=1.0)
1585
1595
1586
- .. figure:: ../../_static/style/bg_axNone_lowhigh .png
1596
+ .. figure:: ../../_static/style/{image_prefix}_axNone_lowhigh .png
1587
1597
1588
1598
Manually setting ``vmin`` and ``vmax`` gradient thresholds
1589
1599
1590
- >>> df.style.background_gradient (axis=None, vmin=6.7, vmax=21.6)
1600
+ >>> df.style.{name}_gradient (axis=None, vmin=6.7, vmax=21.6)
1591
1601
1592
- .. figure:: ../../_static/style/bg_axNone_vminvmax .png
1602
+ .. figure:: ../../_static/style/{image_prefix}_axNone_vminvmax .png
1593
1603
1594
1604
Setting a ``gmap`` and applying to all columns with another ``cmap``
1595
1605
1596
- >>> df.style.background_gradient (axis=0, gmap=df['Temp (c)'], cmap='YlOrRd')
1606
+ >>> df.style.{name}_gradient (axis=0, gmap=df['Temp (c)'], cmap='YlOrRd')
1597
1607
1598
- .. figure:: ../../_static/style/bg_gmap .png
1608
+ .. figure:: ../../_static/style/{image_prefix}_gmap .png
1599
1609
1600
1610
Setting the gradient map for a dataframe (i.e. ``axis=None``), we need to
1601
1611
explicitly state ``subset`` to match the ``gmap`` shape
1602
1612
1603
1613
>>> gmap = np.array([[1,2,3], [2,3,4], [3,4,5]])
1604
- >>> df.style.background_gradient (axis=None, gmap=gmap,
1614
+ >>> df.style.{name}_gradient (axis=None, gmap=gmap,
1605
1615
... cmap='YlOrRd', subset=['Temp (c)', 'Rain (mm)', 'Wind (m/s)']
1606
1616
... )
1607
1617
1608
- .. figure:: ../../_static/style/bg_axNone_gmap .png
1618
+ .. figure:: ../../_static/style/{image_prefix}_axNone_gmap .png
1609
1619
"""
1610
1620
if subset is None and gmap is None :
1611
1621
subset = self .data .select_dtypes (include = np .number ).columns
@@ -1624,6 +1634,41 @@ def background_gradient(
1624
1634
)
1625
1635
return self
1626
1636
1637
+ @doc (
1638
+ background_gradient ,
1639
+ name = "text" ,
1640
+ alt = "background" ,
1641
+ image_prefix = "tg" ,
1642
+ axis = "{0 or 'index', 1 or 'columns', None}" ,
1643
+ text_threshold = "This argument is ignored (only used in `background_gradient`)." ,
1644
+ )
1645
+ def text_gradient (
1646
+ self ,
1647
+ cmap = "PuBu" ,
1648
+ low : float = 0 ,
1649
+ high : float = 0 ,
1650
+ axis : Axis | None = 0 ,
1651
+ subset : Subset | None = None ,
1652
+ vmin : float | None = None ,
1653
+ vmax : float | None = None ,
1654
+ gmap : Sequence | None = None ,
1655
+ ) -> Styler :
1656
+ if subset is None and gmap is None :
1657
+ subset = self .data .select_dtypes (include = np .number ).columns
1658
+
1659
+ return self .apply (
1660
+ _background_gradient ,
1661
+ cmap = cmap ,
1662
+ subset = subset ,
1663
+ axis = axis ,
1664
+ low = low ,
1665
+ high = high ,
1666
+ vmin = vmin ,
1667
+ vmax = vmax ,
1668
+ gmap = gmap ,
1669
+ text_only = True ,
1670
+ )
1671
+
1627
1672
def set_properties (self , subset : Subset | None = None , ** kwargs ) -> Styler :
1628
1673
"""
1629
1674
Set defined CSS-properties to each ``<td>`` HTML element within the given
@@ -2332,6 +2377,7 @@ def _background_gradient(
2332
2377
vmin : float | None = None ,
2333
2378
vmax : float | None = None ,
2334
2379
gmap : Sequence | np .ndarray | FrameOrSeries | None = None ,
2380
+ text_only : bool = False ,
2335
2381
):
2336
2382
"""
2337
2383
Color background in a range according to the data or a gradient map
@@ -2371,16 +2417,19 @@ def relative_luminance(rgba) -> float:
2371
2417
)
2372
2418
return 0.2126 * r + 0.7152 * g + 0.0722 * b
2373
2419
2374
- def css (rgba ) -> str :
2375
- dark = relative_luminance (rgba ) < text_color_threshold
2376
- text_color = "#f1f1f1" if dark else "#000000"
2377
- return f"background-color: { colors .rgb2hex (rgba )} ;color: { text_color } ;"
2420
+ def css (rgba , text_only ) -> str :
2421
+ if not text_only :
2422
+ dark = relative_luminance (rgba ) < text_color_threshold
2423
+ text_color = "#f1f1f1" if dark else "#000000"
2424
+ return f"background-color: { colors .rgb2hex (rgba )} ;color: { text_color } ;"
2425
+ else :
2426
+ return f"color: { colors .rgb2hex (rgba )} ;"
2378
2427
2379
2428
if data .ndim == 1 :
2380
- return [css (rgba ) for rgba in rgbas ]
2429
+ return [css (rgba , text_only ) for rgba in rgbas ]
2381
2430
else :
2382
2431
return DataFrame (
2383
- [[css (rgba ) for rgba in row ] for row in rgbas ],
2432
+ [[css (rgba , text_only ) for rgba in row ] for row in rgbas ],
2384
2433
index = data .index ,
2385
2434
columns = data .columns ,
2386
2435
)
0 commit comments