From c061e7d538b071e0e642ea412b28a9dddf154b56 Mon Sep 17 00:00:00 2001 From: Heather Craker Date: Tue, 12 Jul 2022 12:39:25 -0400 Subject: [PATCH 1/6] Clarified how `vmax` and `Normalize` are used when creating shared colorbars --- .../matplotlib/annotations-colorbars-layouts.ipynb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index e54d9288e..e5223290f 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -426,8 +426,7 @@ "id": "1662bf3c", "metadata": {}, "source": [ - "You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap. \n", - "\n", + "You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap for the first histogram. To make sure that both histograms are using the same colormap with the same range of values, `vmax` is set to 0.18 for both plots. This ensures that both histograms are using colormaps that represent values from 0 (the default for histograms) to 0.18. Because of this, it doesn't matter whether we pass in `hist1[3]` or `hist2[3]` to `fig.colorbar`.\n", "Read more at the [`matplotlib.axes.Axes.hist2d` documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist2d.html)." ] }, @@ -446,7 +445,12 @@ "\n", "The first has very discrete list of colors called `colors`, and creates a colormap from this list with the call `ListedColormap`. \n", "\n", - "The second used the call `LinearSegmentedColormap` to create a colormap from interpolating the same list `colors`." + "The second used the call `LinearSegmentedColormap` to create a colormap from interpolating the same list `colors`.\n", + "\n", + "### The `Normalize` Class\n", + "Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this when ploting the histograms just above! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", + "\n", + "For non-linear nomalization, check out this [Colormap Normalization tutorial](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html#)." ] }, { @@ -628,9 +632,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.4" + "version": "3.10.5" } }, "nbformat": 4, "nbformat_minor": 5 -} +} \ No newline at end of file From 1a7746a6509b55bc073f1e32e63ca8a5d5db3bd1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Jul 2022 16:46:54 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- core/matplotlib/annotations-colorbars-layouts.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index e5223290f..866851270 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -637,4 +637,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From ea33a5e68d973cb350b3cee6cd1471a4fae64e10 Mon Sep 17 00:00:00 2001 From: Heather Craker Date: Tue, 12 Jul 2022 13:24:32 -0400 Subject: [PATCH 3/6] Added contourf example --- .../annotations-colorbars-layouts.ipynb | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index e5223290f..123e5b1e5 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -426,10 +426,54 @@ "id": "1662bf3c", "metadata": {}, "source": [ - "You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap for the first histogram. To make sure that both histograms are using the same colormap with the same range of values, `vmax` is set to 0.18 for both plots. This ensures that both histograms are using colormaps that represent values from 0 (the default for histograms) to 0.18. Because of this, it doesn't matter whether we pass in `hist1[3]` or `hist2[3]` to `fig.colorbar`.\n", + "You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap for the first histogram. To make sure that both histograms are using the same colormap with the same range of values, `vmax` is set to 0.18 for both plots. This ensures that both histograms are using colormaps that represent values from 0 (the default for histograms) to 0.18. Because the same data is used for both plots, it doesn't matter whether we pass in `hist1[3]` or `hist2[3]` to `fig.colorbar`.\n", "Read more at the [`matplotlib.axes.Axes.hist2d` documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist2d.html)." ] }, + { + "cell_type": "markdown", + "id": "84c50862", + "metadata": {}, + "source": [ + "Other kinds of plots can share colorbars too. A common use case is filled contour plots with shared colorbars for comparing data. `vmin` and `vmax` behave the same way for `contourf` as they do for `hist2d`. A downside to using the `vmin` and `vmax` kwargs when plotting two different datasets is that while the colormaps may be the same, the dataset with a smaller range of values won't show the full range of colors as seen below. Thus, it does matter in this particular example which output from `contourf` is used to make the colorbar." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28d4cea3", + "metadata": {}, + "outputs": [], + "source": [ + "x2 = y2 = np.arange(-3, 3.01, 0.025)\n", + "X2, Y2 = np.meshgrid(x2, y2)\n", + "Z = np.sqrt(np.sin(X2)**2 + np.sin(Y2)**2)\n", + "Z2 = np.sqrt(2*np.cos(X2)**2 + 2*np.cos(Y2)**2)\n", + "\n", + "fig, ax = plt.subplots(nrows=1, ncols=2, constrained_layout=True)\n", + "c1 = ax[0].contourf(X2, Y2, Z, vmin=0, vmax=2)\n", + "c2 = ax[1].contourf(X2, Y2, Z2, vmin=0, vmax=2)\n", + "fig.colorbar(c1, ax=ax[0], location='bottom')\n", + "fig.colorbar(c2, ax=ax[1], location='bottom')\n", + "\n", + "fig.suptitle('Shared colormaps on data with different ranges')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5570ebb7", + "metadata": {}, + "outputs": [], + "source": [ + "fig, ax = plt.subplots(nrows=1, ncols=2, constrained_layout=True)\n", + "c1 = ax[0].contourf(X2, Y2, Z, vmin=0, vmax=2)\n", + "c2 = ax[1].contourf(X2, Y2, Z2, vmin=0, vmax=2)\n", + "fig.colorbar(c2, ax=ax, location='bottom')\n", + "\n", + "fig.suptitle('Using the contourf output from the data with a wider range')" + ] + }, { "cell_type": "markdown", "id": "92d072f8-7370-4ea5-92e0-4407cb5905bb", From de8f44945b3081f696686ff9fc1bcdaf8db8a909 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Jul 2022 17:25:25 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- core/matplotlib/annotations-colorbars-layouts.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index 9ffa7886b..3848c439d 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -447,8 +447,8 @@ "source": [ "x2 = y2 = np.arange(-3, 3.01, 0.025)\n", "X2, Y2 = np.meshgrid(x2, y2)\n", - "Z = np.sqrt(np.sin(X2)**2 + np.sin(Y2)**2)\n", - "Z2 = np.sqrt(2*np.cos(X2)**2 + 2*np.cos(Y2)**2)\n", + "Z = np.sqrt(np.sin(X2) ** 2 + np.sin(Y2) ** 2)\n", + "Z2 = np.sqrt(2 * np.cos(X2) ** 2 + 2 * np.cos(Y2) ** 2)\n", "\n", "fig, ax = plt.subplots(nrows=1, ncols=2, constrained_layout=True)\n", "c1 = ax[0].contourf(X2, Y2, Z, vmin=0, vmax=2)\n", From 24387ffd154cda3a1f2fcc5dbde0e0537999ca91 Mon Sep 17 00:00:00 2001 From: Heather Craker Date: Thu, 14 Jul 2022 16:33:23 -0400 Subject: [PATCH 5/6] Addressed Robert's suggestions --- .../annotations-colorbars-layouts.ipynb | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index 3848c439d..d7463d219 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -435,7 +435,7 @@ "id": "84c50862", "metadata": {}, "source": [ - "Other kinds of plots can share colorbars too. A common use case is filled contour plots with shared colorbars for comparing data. `vmin` and `vmax` behave the same way for `contourf` as they do for `hist2d`. A downside to using the `vmin` and `vmax` kwargs when plotting two different datasets is that while the colormaps may be the same, the dataset with a smaller range of values won't show the full range of colors as seen below. Thus, it does matter in this particular example which output from `contourf` is used to make the colorbar." + "Other kinds of plots can share colorbars too. A common use case is filled contour plots with shared colorbars for comparing data. `vmin` and `vmax` behave the same way for `contourf` as they do for `hist2d`. A downside to using the `vmin` and `vmax` kwargs when plotting two different datasets is that while the colormaps may be the same, the dataset with a smaller range of values won't show the full range of colors as seen below. Thus, it *does* matter in this particular example which output from `contourf` is used to make the colorbar." ] }, { @@ -489,12 +489,7 @@ "\n", "The first has very discrete list of colors called `colors`, and creates a colormap from this list with the call `ListedColormap`. \n", "\n", - "The second used the call `LinearSegmentedColormap` to create a colormap from interpolating the same list `colors`.\n", - "\n", - "### The `Normalize` Class\n", - "Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this when ploting the histograms just above! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", - "\n", - "For non-linear nomalization, check out this [Colormap Normalization tutorial](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html#)." + "The second used the call `LinearSegmentedColormap` to create a colormap from interpolating the same list `colors`." ] }, { @@ -543,6 +538,17 @@ "cbar = fig.colorbar(hist1[3], ax=ax, location='bottom')" ] }, + { + "cell_type": "markdown", + "id": "ea7f200f", + "metadata": {}, + "source": [ + "### The `Normalize` Class\n", + "Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this when plotting the histograms just above! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", + "\n", + "For non-linear nomalization, check out this [Colormap Normalization tutorial](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html#)." + ] + }, { "cell_type": "markdown", "id": "e41f44e0-2c4f-4ce2-abe6-35d20b8c142e", From dc1e298641b39bdd3a0eff8158528d4442fd5bb8 Mon Sep 17 00:00:00 2001 From: Heather Craker Date: Fri, 22 Jul 2022 10:22:38 -0400 Subject: [PATCH 6/6] Addressed Max's suggestions --- core/matplotlib/annotations-colorbars-layouts.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/matplotlib/annotations-colorbars-layouts.ipynb b/core/matplotlib/annotations-colorbars-layouts.ipynb index d7463d219..10500c98a 100644 --- a/core/matplotlib/annotations-colorbars-layouts.ipynb +++ b/core/matplotlib/annotations-colorbars-layouts.ipynb @@ -544,7 +544,7 @@ "metadata": {}, "source": [ "### The `Normalize` Class\n", - "Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this when plotting the histograms just above! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", + "Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this functionality in the previous histogram exercise! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", "\n", "For non-linear nomalization, check out this [Colormap Normalization tutorial](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html#)." ]