|
1006 | 1006 | "cell_type": "markdown",
|
1007 | 1007 | "metadata": {},
|
1008 | 1008 | "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" |
1010 | 1033 | ]
|
1011 | 1034 | },
|
1012 | 1035 | {
|
|
1017 | 1040 | "source": [
|
1018 | 1041 | "df2.iloc[0,2] = np.nan\n",
|
1019 | 1042 | "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')" |
1021 | 1044 | ]
|
1022 | 1045 | },
|
1023 | 1046 | {
|
1024 | 1047 | "cell_type": "markdown",
|
1025 | 1048 | "metadata": {},
|
1026 | 1049 | "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" |
1028 | 1051 | ]
|
1029 | 1052 | },
|
1030 | 1053 | {
|
|
1033 | 1056 | "metadata": {},
|
1034 | 1057 | "outputs": [],
|
1035 | 1058 | "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;')" |
1040 | 1060 | ]
|
1041 | 1061 | },
|
1042 | 1062 | {
|
1043 | 1063 | "cell_type": "markdown",
|
1044 | 1064 | "metadata": {},
|
1045 | 1065 | "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." |
1047 | 1068 | ]
|
1048 | 1069 | },
|
1049 | 1070 | {
|
|
1052 | 1073 | "metadata": {},
|
1053 | 1074 | "outputs": [],
|
1054 | 1075 | "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" |
1057 | 1086 | ]
|
1058 | 1087 | },
|
1059 | 1088 | {
|
|
1062 | 1091 | "metadata": {},
|
1063 | 1092 | "outputs": [],
|
1064 | 1093 | "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" |
1069 | 1102 | ]
|
1070 | 1103 | },
|
1071 | 1104 | {
|
1072 | 1105 | "cell_type": "markdown",
|
1073 | 1106 | "metadata": {},
|
1074 | 1107 | "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." |
1076 | 1109 | ]
|
1077 | 1110 | },
|
1078 | 1111 | {
|
|
1081 | 1114 | "metadata": {},
|
1082 | 1115 | "outputs": [],
|
1083 | 1116 | "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" |
1085 | 1130 | ]
|
1086 | 1131 | },
|
1087 | 1132 | {
|
|
0 commit comments