Skip to content

Commit f502748

Browse files
authored
DOC: update style.ipynb user guide for recent enhancements (pandas-dev#41013)
1 parent 11545d5 commit f502748

File tree

1 file changed

+61
-16
lines changed

1 file changed

+61
-16
lines changed

doc/source/user_guide/style.ipynb

+61-16
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,30 @@
10061006
"cell_type": "markdown",
10071007
"metadata": {},
10081008
"source": [
1009-
"We expect certain styling functions to be common enough that we've included a few \"built-in\" to the `Styler`, so you don't have to write them yourself."
1009+
"Some styling functions are common enough that we've \"built them in\" to the `Styler`, so you don't have to write them and apply them yourself. The current list of such functions is:\n",
1010+
"\n",
1011+
" - [.highlight_null][nullfunc]: for use with identifying missing data. \n",
1012+
" - [.highlight_min][minfunc] and [.highlight_max][maxfunc]: for use with identifying extremeties in data.\n",
1013+
" - [.highlight_between][betweenfunc] and [.highlight_quantile][quantilefunc]: for use with identifying classes within data.\n",
1014+
" - [.background_gradient][bgfunc]: a flexible method for highlighting cells based or their, or other, values on a numeric scale.\n",
1015+
" - [.bar][barfunc]: to display mini-charts within cell backgrounds.\n",
1016+
" \n",
1017+
"The individual documentation on each function often gives more examples of their arguments.\n",
1018+
"\n",
1019+
"[nullfunc]: ../reference/api/pandas.io.formats.style.Styler.highlight_null.rst\n",
1020+
"[minfunc]: ../reference/api/pandas.io.formats.style.Styler.highlight_min.rst\n",
1021+
"[maxfunc]: ../reference/api/pandas.io.formats.style.Styler.highlight_max.rst\n",
1022+
"[betweenfunc]: ../reference/api/pandas.io.formats.style.Styler.highlight_between.rst\n",
1023+
"[quantilefunc]: ../reference/api/pandas.io.formats.style.Styler.highlight_quantile.rst\n",
1024+
"[bgfunc]: ../reference/api/pandas.io.formats.style.Styler.background_gradient.rst\n",
1025+
"[barfunc]: ../reference/api/pandas.io.formats.style.Styler.bar.rst"
1026+
]
1027+
},
1028+
{
1029+
"cell_type": "markdown",
1030+
"metadata": {},
1031+
"source": [
1032+
"### Highlight Null"
10101033
]
10111034
},
10121035
{
@@ -1017,14 +1040,14 @@
10171040
"source": [
10181041
"df2.iloc[0,2] = np.nan\n",
10191042
"df2.iloc[4,3] = np.nan\n",
1020-
"df2.loc[:4].style.highlight_null(null_color='red')"
1043+
"df2.loc[:4].style.highlight_null(null_color='yellow')"
10211044
]
10221045
},
10231046
{
10241047
"cell_type": "markdown",
10251048
"metadata": {},
10261049
"source": [
1027-
"You can create \"heatmaps\" with the `background_gradient` method. These require matplotlib, and we'll use [Seaborn](https://stanford.edu/~mwaskom/software/seaborn/) to get a nice colormap."
1050+
"### Highlight Min or Max"
10281051
]
10291052
},
10301053
{
@@ -1033,17 +1056,15 @@
10331056
"metadata": {},
10341057
"outputs": [],
10351058
"source": [
1036-
"import seaborn as sns\n",
1037-
"cm = sns.light_palette(\"green\", as_cmap=True)\n",
1038-
"\n",
1039-
"df2.style.background_gradient(cmap=cm)"
1059+
"df2.loc[:4].style.highlight_max(axis=1, props='color:white; font-weight:bold; background-color:darkblue;')"
10401060
]
10411061
},
10421062
{
10431063
"cell_type": "markdown",
10441064
"metadata": {},
10451065
"source": [
1046-
"`Styler.background_gradient` takes the keyword arguments `low` and `high`. Roughly speaking these extend the range of your data by `low` and `high` percent so that when we convert the colors, the colormap's entire range isn't used. This is useful so that you can actually read the text still."
1066+
"### Highlight Between\n",
1067+
"This method accepts ranges as float, or NumPy arrays or Series provided the indexes match."
10471068
]
10481069
},
10491070
{
@@ -1052,8 +1073,16 @@
10521073
"metadata": {},
10531074
"outputs": [],
10541075
"source": [
1055-
"# Uses the full color range\n",
1056-
"df2.loc[:4].style.background_gradient(cmap='viridis')"
1076+
"left = pd.Series([1.0, 0.0, 1.0], index=[\"A\", \"B\", \"D\"])\n",
1077+
"df2.loc[:4].style.highlight_between(left=left, right=1.5, axis=1, props='color:white; background-color:purple;')"
1078+
]
1079+
},
1080+
{
1081+
"cell_type": "markdown",
1082+
"metadata": {},
1083+
"source": [
1084+
"### Highlight Quantile\n",
1085+
"Useful for detecting the highest or lowest percentile values"
10571086
]
10581087
},
10591088
{
@@ -1062,17 +1091,21 @@
10621091
"metadata": {},
10631092
"outputs": [],
10641093
"source": [
1065-
"# Compress the color range\n",
1066-
"df2.loc[:4].style\\\n",
1067-
" .background_gradient(cmap='viridis', low=.5, high=0)\\\n",
1068-
" .highlight_null('red')"
1094+
"df2.loc[:4].style.highlight_quantile(q_left=0.85, axis=None, color='yellow')"
1095+
]
1096+
},
1097+
{
1098+
"cell_type": "markdown",
1099+
"metadata": {},
1100+
"source": [
1101+
"### Background Gradient"
10691102
]
10701103
},
10711104
{
10721105
"cell_type": "markdown",
10731106
"metadata": {},
10741107
"source": [
1075-
"There's also `.highlight_min` and `.highlight_max`, which is almost identical to the user defined version we created above, and also a `.highlight_null` method. "
1108+
"You can create \"heatmaps\" with the `background_gradient` method. These require matplotlib, and we'll use [Seaborn](https://stanford.edu/~mwaskom/software/seaborn/) to get a nice colormap."
10761109
]
10771110
},
10781111
{
@@ -1081,7 +1114,19 @@
10811114
"metadata": {},
10821115
"outputs": [],
10831116
"source": [
1084-
"df2.loc[:4].style.highlight_max(axis=0)"
1117+
"import seaborn as sns\n",
1118+
"cm = sns.light_palette(\"green\", as_cmap=True)\n",
1119+
"\n",
1120+
"df2.style.background_gradient(cmap=cm)"
1121+
]
1122+
},
1123+
{
1124+
"cell_type": "markdown",
1125+
"metadata": {},
1126+
"source": [
1127+
"[.background_gradient][bgfunc] has a number of keyword arguments to customise the gradients and colors. See its documentation.\n",
1128+
"\n",
1129+
"[bgfunc]: ../reference/api/pandas.io.formats.style.Styler.background_gradient.rst"
10851130
]
10861131
},
10871132
{

0 commit comments

Comments
 (0)