From a49fac51b1a6eb22012dba7a1469ae61db1edfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sat, 24 May 2025 15:51:39 +0000 Subject: [PATCH 01/21] Rename evans_test.py example for Foo units class and conversion --- galleries/examples/units/{evans_test.py => test_evans.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename galleries/examples/units/{evans_test.py => test_evans.py} (100%) diff --git a/galleries/examples/units/evans_test.py b/galleries/examples/units/test_evans.py similarity index 100% rename from galleries/examples/units/evans_test.py rename to galleries/examples/units/test_evans.py From 87e8e37b9301db849fe96846c5a37a6e0c98c1a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sat, 24 May 2025 21:25:13 +0000 Subject: [PATCH 02/21] Rename test_evans to unit_conversion_for_class_Foo --- .../units/{test_evans.py => unit_conversion_for_class_Foo.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename galleries/examples/units/{test_evans.py => unit_conversion_for_class_Foo.py} (100%) diff --git a/galleries/examples/units/test_evans.py b/galleries/examples/units/unit_conversion_for_class_Foo.py similarity index 100% rename from galleries/examples/units/test_evans.py rename to galleries/examples/units/unit_conversion_for_class_Foo.py From 4df2b3985841cc7ddc2020627bb733b1dd291d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 14:17:55 +0000 Subject: [PATCH 03/21] back to evans_test.py --- .../units/{unit_conversion_for_class_Foo.py => evans_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename galleries/examples/units/{unit_conversion_for_class_Foo.py => evans_test.py} (100%) diff --git a/galleries/examples/units/unit_conversion_for_class_Foo.py b/galleries/examples/units/evans_test.py similarity index 100% rename from galleries/examples/units/unit_conversion_for_class_Foo.py rename to galleries/examples/units/evans_test.py From 646be10edfe2a82e0be9b8521659308a2589909c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 14:25:08 +0000 Subject: [PATCH 04/21] Add colorbar histogram example to README and implement the example script --- galleries/examples/color/README.txt | 2 + .../examples/color/colorbar_histogram.py | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 galleries/examples/color/colorbar_histogram.py diff --git a/galleries/examples/color/README.txt b/galleries/examples/color/README.txt index 4b8b3bc4b751..8085271219e8 100644 --- a/galleries/examples/color/README.txt +++ b/galleries/examples/color/README.txt @@ -5,3 +5,5 @@ Color For a description of the colormaps available in Matplotlib, see the :ref:`colormaps tutorial `. + +- colorbar_histogram.py: Example of a colorbar with a histogram inset. diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py new file mode 100644 index 000000000000..ca90104dbb44 --- /dev/null +++ b/galleries/examples/color/colorbar_histogram.py @@ -0,0 +1,42 @@ +""" +========================= +Colorbar with Histogram +========================= + +This example demonstrates how to create a colorbar for an image and +add a histogram of the data values alongside it. This is useful for +visualizing the distribution of values mapped to colors. + +""" + +import numpy as np +import matplotlib.pyplot as plt +import matplotlib.colors as mcolors + +# Generate random data +x = np.random.random(100).reshape(10, 10) + +# Compute histogram +counts, bins = np.histogram(x) + +# Set up colormap and normalization +cmap = plt.colormaps['viridis'] +norm = mcolors.BoundaryNorm(bins, cmap.N) + +fig, ax = plt.subplots() +im = ax.imshow(x, cmap=cmap, norm=norm) +cbar = plt.colorbar(im, ax=ax) +cbar.set_label('Value') + +# Add histogram as an inset axis +cax = ax.inset_axes([1.05, 0, .25, 1]) +midpoints = bins[:-1] + np.diff(bins) / 2 +cax.barh(midpoints, counts, height=1/len(counts), color=cmap(norm(midpoints))) +cax.set_yticks(bins) +cax.margins(0) +for spine in cax.spines.values(): + spine.set_visible(False) +cax.set_xlabel('Count') +cax.set_ylabel('Value') + +plt.show() \ No newline at end of file From c81072f5c296f303f02164b84db72032c14f9f7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 14:28:48 +0000 Subject: [PATCH 05/21] Add completion message to colorbar histogram example --- galleries/examples/color/colorbar_histogram.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index ca90104dbb44..152604d49a7d 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -39,4 +39,6 @@ cax.set_xlabel('Count') cax.set_ylabel('Value') -plt.show() \ No newline at end of file +plt.show() + +print("Colorbar with histogram example completed.") \ No newline at end of file From 19a2d19ed17845aa357ea3e84462998badc0d777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 15:25:07 +0000 Subject: [PATCH 06/21] Fix newline at end of file in colorbar histogram example --- galleries/examples/color/colorbar_histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 152604d49a7d..f3d4ec30f20c 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -41,4 +41,4 @@ plt.show() -print("Colorbar with histogram example completed.") \ No newline at end of file +print("Colorbar with histogram example completed.") From 9931dba05d057e13825015c491c30d14e662fd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 17:50:02 +0000 Subject: [PATCH 07/21] Refactor colorbar histogram example to use constrained layout and improve spacing --- .../examples/color/colorbar_histogram.py | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index f3d4ec30f20c..dffb6b5eec5b 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -12,6 +12,7 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors +from matplotlib import gridspec # Generate random data x = np.random.random(100).reshape(10, 10) @@ -23,21 +24,30 @@ cmap = plt.colormaps['viridis'] norm = mcolors.BoundaryNorm(bins, cmap.N) -fig, ax = plt.subplots() -im = ax.imshow(x, cmap=cmap, norm=norm) -cbar = plt.colorbar(im, ax=ax) +# Create figure with constrained_layout for better spacing +fig = plt.figure(figsize=(8, 4), constrained_layout=True) +gs = gridspec.GridSpec(1, 3, width_ratios=[4, 0.2, 1], figure=fig) + +# Main image +ax_img = fig.add_subplot(gs[0]) +im = ax_img.imshow(x, cmap=cmap, norm=norm) +ax_img.set_title("Image") + +# Colorbar +cax = fig.add_subplot(gs[1]) +cbar = plt.colorbar(im, cax=cax) cbar.set_label('Value') -# Add histogram as an inset axis -cax = ax.inset_axes([1.05, 0, .25, 1]) +# Histogram +ax_hist = fig.add_subplot(gs[2]) midpoints = bins[:-1] + np.diff(bins) / 2 -cax.barh(midpoints, counts, height=1/len(counts), color=cmap(norm(midpoints))) -cax.set_yticks(bins) -cax.margins(0) -for spine in cax.spines.values(): +ax_hist.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints)), edgecolor='k') +ax_hist.set_yticks(bins) +ax_hist.set_xlabel('Count') +ax_hist.set_ylabel('Value') +ax_hist.margins(0) +for spine in ax_hist.spines.values(): spine.set_visible(False) -cax.set_xlabel('Count') -cax.set_ylabel('Value') plt.show() From 578e41492d981b9d6acb6bdba4a3a99103b0eeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Sun, 25 May 2025 17:56:08 +0000 Subject: [PATCH 08/21] Refactor colorbar histogram example to improve layout and streamline code --- .../examples/color/colorbar_histogram.py | 42 ++++++++----------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index dffb6b5eec5b..41f5e70ca8ac 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -12,43 +12,35 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors -from matplotlib import gridspec +from mpl_toolkits.axes_grid1 import make_axes_locatable # Generate random data -x = np.random.random(100).reshape(10, 10) +x = np.random.random((10, 10)) # Compute histogram counts, bins = np.histogram(x) # Set up colormap and normalization -cmap = plt.colormaps['viridis'] +cmap = plt.get_cmap('viridis') norm = mcolors.BoundaryNorm(bins, cmap.N) -# Create figure with constrained_layout for better spacing -fig = plt.figure(figsize=(8, 4), constrained_layout=True) -gs = gridspec.GridSpec(1, 3, width_ratios=[4, 0.2, 1], figure=fig) - -# Main image -ax_img = fig.add_subplot(gs[0]) -im = ax_img.imshow(x, cmap=cmap, norm=norm) -ax_img.set_title("Image") - -# Colorbar -cax = fig.add_subplot(gs[1]) -cbar = plt.colorbar(im, cax=cax) +fig, ax = plt.subplots() +im = ax.imshow(x, cmap=cmap, norm=norm) +cbar = plt.colorbar(im, ax=ax) cbar.set_label('Value') -# Histogram -ax_hist = fig.add_subplot(gs[2]) +# Create an axes on the right side of ax. The width of cax will be 20% of ax and the padding between cax and ax will be fixed at 0.05 inch. +divider = make_axes_locatable(ax) +cax = divider.append_axes("right", size="20%", pad=0.05) + +# Plot histogram midpoints = bins[:-1] + np.diff(bins) / 2 -ax_hist.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints)), edgecolor='k') -ax_hist.set_yticks(bins) -ax_hist.set_xlabel('Count') -ax_hist.set_ylabel('Value') -ax_hist.margins(0) -for spine in ax_hist.spines.values(): - spine.set_visible(False) +cax.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints))) +cax.set_yticks(bins) +cax.set_xlabel('Count') +cax.set_ylabel('Value') +cax.invert_yaxis() # Optional: to match the orientation of imshow +plt.tight_layout() plt.show() -print("Colorbar with histogram example completed.") From 0089861e452115d3221af2697ed72a67186d01dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Mon, 26 May 2025 00:07:03 +0000 Subject: [PATCH 09/21] Update colorbar histogram example: rename title, improve inset axes layout, and adjust label positions --- galleries/examples/color/README.txt | 2 -- .../examples/color/colorbar_histogram.py | 35 +++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/galleries/examples/color/README.txt b/galleries/examples/color/README.txt index 8085271219e8..4b8b3bc4b751 100644 --- a/galleries/examples/color/README.txt +++ b/galleries/examples/color/README.txt @@ -5,5 +5,3 @@ Color For a description of the colormaps available in Matplotlib, see the :ref:`colormaps tutorial `. - -- colorbar_histogram.py: Example of a colorbar with a histogram inset. diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 41f5e70ca8ac..e649741842ec 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -1,6 +1,6 @@ """ ========================= -Colorbar with Histogram +Histogram as Colorbar ========================= This example demonstrates how to create a colorbar for an image and @@ -8,11 +8,10 @@ visualizing the distribution of values mapped to colors. """ - import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors -from mpl_toolkits.axes_grid1 import make_axes_locatable +from mpl_toolkits.axes_grid1.inset_locator import inset_axes # Generate random data x = np.random.random((10, 10)) @@ -26,21 +25,29 @@ fig, ax = plt.subplots() im = ax.imshow(x, cmap=cmap, norm=norm) -cbar = plt.colorbar(im, ax=ax) -cbar.set_label('Value') -# Create an axes on the right side of ax. The width of cax will be 20% of ax and the padding between cax and ax will be fixed at 0.05 inch. -divider = make_axes_locatable(ax) -cax = divider.append_axes("right", size="20%", pad=0.05) +# Create an inset axes for the histogram +cax = inset_axes(ax, + width="20%", + height="95%", + loc='center left', + bbox_to_anchor=(1.18, 0.025, 1, 1), # x-shift, y-shift, width, height + bbox_transform=ax.transAxes, + borderpad=0) # Plot histogram midpoints = bins[:-1] + np.diff(bins) / 2 -cax.barh(midpoints, counts, height=np.diff(bins), color=cmap(norm(midpoints))) +bar_height = np.min(np.diff(bins)) +cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints))) + +# Adjust label distances more precisely +cax.set_xlabel('Count', labelpad=3) +cax.set_ylabel('Value', labelpad=2) + + cax.set_yticks(bins) -cax.set_xlabel('Count') -cax.set_ylabel('Value') -cax.invert_yaxis() # Optional: to match the orientation of imshow +cax.invert_yaxis() # Optional: match image orientation -plt.tight_layout() +# Leave room for histogram inset +plt.subplots_adjust(right=0.75) plt.show() - From bf6f2e8c085bb3523c40bc5f4aacc7a85e945340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Mon, 26 May 2025 00:38:37 +0000 Subject: [PATCH 10/21] Plotting a different database + refactor bins to np.linespace(-1,1,11) + remove yaxis inversion --- .../examples/color/colorbar_histogram.py | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index e649741842ec..fa707be4c408 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -13,41 +13,51 @@ import matplotlib.colors as mcolors from mpl_toolkits.axes_grid1.inset_locator import inset_axes -# Generate random data -x = np.random.random((10, 10)) - -# Compute histogram -counts, bins = np.histogram(x) - -# Set up colormap and normalization -cmap = plt.get_cmap('viridis') +# === Surface Data === +delta = 0.025 +x = y = np.arange(-3.0, 3.0, delta) +X, Y = np.meshgrid(x, y) +Z1 = np.exp(-X**2 - Y**2) +Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) +Z = (Z1 - Z2) * 2 + +# Purposeful bins for full value range +bins = np.linspace(-1, 1, 11) +counts, _ = np.histogram(Z, bins=bins) + +# Normalize to bins for colormap +cmap = plt.get_cmap('RdYlBu') norm = mcolors.BoundaryNorm(bins, cmap.N) +# === Plot heatmap === fig, ax = plt.subplots() -im = ax.imshow(x, cmap=cmap, norm=norm) +im = ax.imshow(Z, interpolation='bilinear', cmap=cmap, + origin='lower', extent=[-3, 3, -3, 3], + norm=norm) -# Create an inset axes for the histogram +# === Inset histogram as colorbar === cax = inset_axes(ax, - width="20%", - height="95%", + width="20%", + height="95%", loc='center left', - bbox_to_anchor=(1.18, 0.025, 1, 1), # x-shift, y-shift, width, height + bbox_to_anchor=(1.18, 0.025, 1, 1), bbox_transform=ax.transAxes, borderpad=0) -# Plot histogram +# Histogram values and bars midpoints = bins[:-1] + np.diff(bins) / 2 bar_height = np.min(np.diff(bins)) cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints))) +cax.margins(0) -# Adjust label distances more precisely -cax.set_xlabel('Count', labelpad=3) -cax.set_ylabel('Value', labelpad=2) - +# Axis labels with clean spacing +cax.set_xlabel('Count', labelpad=6) +cax.set_ylabel('Value', labelpad=2) +# Use bin edges as y-ticks (no inversion now) cax.set_yticks(bins) -cax.invert_yaxis() # Optional: match image orientation # Leave room for histogram inset plt.subplots_adjust(right=0.75) -plt.show() + +plt.show() \ No newline at end of file From 9c5c859f60c94138426b1378647998be5851d234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Mon, 26 May 2025 14:07:10 +0000 Subject: [PATCH 11/21] Refactor colorbar histogram example: streamline histogram calculation, improve inset axes layout, and enhance label spacing --- .../examples/color/colorbar_histogram.py | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index fa707be4c408..52477b32ad88 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -11,7 +11,6 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors -from mpl_toolkits.axes_grid1.inset_locator import inset_axes # === Surface Data === delta = 0.025 @@ -21,43 +20,39 @@ Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) * 2 -# Purposeful bins for full value range -bins = np.linspace(-1, 1, 11) -counts, _ = np.histogram(Z, bins=bins) +# === Histogram from actual Z data === +counts, bins = np.histogram(Z, bins=30) -# Normalize to bins for colormap +# === Colormap & Normalization === cmap = plt.get_cmap('RdYlBu') norm = mcolors.BoundaryNorm(bins, cmap.N) -# === Plot heatmap === +# === Main Plot === fig, ax = plt.subplots() im = ax.imshow(Z, interpolation='bilinear', cmap=cmap, origin='lower', extent=[-3, 3, -3, 3], norm=norm) -# === Inset histogram as colorbar === -cax = inset_axes(ax, - width="20%", - height="95%", - loc='center left', - bbox_to_anchor=(1.18, 0.025, 1, 1), - bbox_transform=ax.transAxes, - borderpad=0) +# === Inset Histogram Using ax.inset_axes === +cax = ax.inset_axes([1.28, 0.05, 0.25, 1.2]) -# Histogram values and bars +# === Plot Histogram === midpoints = bins[:-1] + np.diff(bins) / 2 -bar_height = np.min(np.diff(bins)) +bar_height = 1 / len(counts) cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints))) + +# === Remove spines and margins === +for spine in cax.spines.values(): + spine.set_visible(False) cax.margins(0) -# Axis labels with clean spacing -cax.set_xlabel('Count', labelpad=6) -cax.set_ylabel('Value', labelpad=2) +# Optional: clean up ticks +cax.tick_params(axis='both', which='both', length=0) -# Use bin edges as y-ticks (no inversion now) +# === Labels and ticks === +cax.set_xlabel('Count', labelpad=8) # increased from 6 → 8 +cax.set_ylabel('Value', labelpad=10) # increased from 8 → 10 cax.set_yticks(bins) -# Leave room for histogram inset -plt.subplots_adjust(right=0.75) +plt.show() -plt.show() \ No newline at end of file From 96781f3262342775e4b9bfe67d106090c9c966ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Mon, 26 May 2025 21:47:52 +0000 Subject: [PATCH 12/21] Refactor colorbar histogram example: adjust inset histogram positioning, improve layout, and clean up axis labels --- .../examples/color/colorbar_histogram.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 52477b32ad88..58f5a66d9a82 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -11,6 +11,7 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors +from matplotlib.ticker import MaxNLocator # === Surface Data === delta = 0.025 @@ -33,26 +34,30 @@ origin='lower', extent=[-3, 3, -3, 3], norm=norm) -# === Inset Histogram Using ax.inset_axes === -cax = ax.inset_axes([1.28, 0.05, 0.25, 1.2]) +# Adjust image position to allow space +plt.subplots_adjust(right=0.78, top=0.92, bottom=0.08) + +# === Inset Histogram – Positioning adjusted === +cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height # === Plot Histogram === midpoints = bins[:-1] + np.diff(bins) / 2 bar_height = 1 / len(counts) cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints))) -# === Remove spines and margins === +# === Clean up === for spine in cax.spines.values(): spine.set_visible(False) cax.margins(0) - -# Optional: clean up ticks cax.tick_params(axis='both', which='both', length=0) -# === Labels and ticks === -cax.set_xlabel('Count', labelpad=8) # increased from 6 → 8 -cax.set_ylabel('Value', labelpad=10) # increased from 8 → 10 +# === Axis labels === +cax.set_xlabel('Count', labelpad=10) +cax.set_ylabel('Value', labelpad=6) + +# === Ticks === cax.set_yticks(bins) +cax.yaxis.set_major_locator(MaxNLocator(nbins=8)) plt.show() From 6062d917c2fc535cf58f4b541ddba162e2c12772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 00:20:56 +0000 Subject: [PATCH 13/21] Update histogram bar height calculation for improved visualization --- galleries/examples/color/colorbar_histogram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 58f5a66d9a82..6b664c32225f 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -43,7 +43,7 @@ # === Plot Histogram === midpoints = bins[:-1] + np.diff(bins) / 2 bar_height = 1 / len(counts) -cax.barh(midpoints, counts, height=bar_height, color=cmap(norm(midpoints))) +cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints))) # === Clean up === for spine in cax.spines.values(): From 69a388feef64ef26d3f4c950946671bcfe6ba998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 09:08:38 -0300 Subject: [PATCH 14/21] Update colorbar_histogram.py Co-authored-by: hannah --- galleries/examples/color/colorbar_histogram.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 6b664c32225f..be8dc74017a5 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -46,8 +46,7 @@ cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints))) # === Clean up === -for spine in cax.spines.values(): - spine.set_visible(False) +cax.spines[:].set_visible(False) cax.margins(0) cax.tick_params(axis='both', which='both', length=0) From b3d7b4242bb39ec36be4905cc750dc92171102bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 12:22:12 +0000 Subject: [PATCH 15/21] Refactor inset histogram cleanup: remove unnecessary comment and enhance code clarity --- galleries/examples/color/colorbar_histogram.py | 1 + 1 file changed, 1 insertion(+) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index be8dc74017a5..ca5a8c733249 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -46,6 +46,7 @@ cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints))) # === Clean up === +#showcase none loop version cax.spines[:].set_visible(False) cax.margins(0) cax.tick_params(axis='both', which='both', length=0) From 80047423579b3d7a92f6592212fee5bd673d8bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 12:24:45 +0000 Subject: [PATCH 16/21] Refactor inset histogram cleanup: remove unnecessary comment to enhance code clarity --- galleries/examples/color/colorbar_histogram.py | 1 - 1 file changed, 1 deletion(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index ca5a8c733249..be8dc74017a5 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -46,7 +46,6 @@ cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints))) # === Clean up === -#showcase none loop version cax.spines[:].set_visible(False) cax.margins(0) cax.tick_params(axis='both', which='both', length=0) From f3e8acfbbb22b0c9be6c993df619480b2c24d24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 21:59:29 +0000 Subject: [PATCH 17/21] Using a new histogram + swapping blue and red in cmap --- galleries/examples/color/colorbar_histogram.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index be8dc74017a5..dba832ca5238 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -17,15 +17,15 @@ delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) -Z1 = np.exp(-X**2 - Y**2) -Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) -Z = (Z1 - Z2) * 2 +Z1 = np.exp(-((X+1)*1.3)**2 - ((Y+1)*1.3)**2) +Z2 = 2.5*np.exp(-(X - 1)**2 - (Y - 1)**2) +Z = (Z1**0.25 - Z2**0.5) # === Histogram from actual Z data === counts, bins = np.histogram(Z, bins=30) # === Colormap & Normalization === -cmap = plt.get_cmap('RdYlBu') +cmap = plt.get_cmap('RdYlBu_r') norm = mcolors.BoundaryNorm(bins, cmap.N) # === Main Plot === From cf4f62caab090ed72010f009aa3f7cdbc15da043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 22:45:20 +0000 Subject: [PATCH 18/21] Fix histogram range and improve image display: adjust x range and remove interpolation in imshow --- galleries/examples/color/colorbar_histogram.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index dba832ca5238..fc4271847b92 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -15,7 +15,7 @@ # === Surface Data === delta = 0.025 -x = y = np.arange(-3.0, 3.0, delta) +x = y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = np.exp(-((X+1)*1.3)**2 - ((Y+1)*1.3)**2) Z2 = 2.5*np.exp(-(X - 1)**2 - (Y - 1)**2) @@ -30,7 +30,7 @@ # === Main Plot === fig, ax = plt.subplots() -im = ax.imshow(Z, interpolation='bilinear', cmap=cmap, +im = ax.imshow(Z, cmap=cmap, origin='lower', extent=[-3, 3, -3, 3], norm=norm) @@ -42,8 +42,10 @@ # === Plot Histogram === midpoints = bins[:-1] + np.diff(bins) / 2 -bar_height = 1 / len(counts) -cax.barh(midpoints, counts, height=np.median(np.diff(bins))*0.8, color=cmap(norm(midpoints))) +height = np.median(np.diff(bins)) * 0.8 +colors = cmap(norm(midpoints)) + +cax.barh(midpoints, counts, height=height, color=colors) # === Clean up === cax.spines[:].set_visible(False) From 767ec0e07a4f5f9c22902b6bdc8e000b266bbc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Tue, 27 May 2025 21:54:04 -0300 Subject: [PATCH 19/21] Update galleries/examples/color/colorbar_histogram.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- galleries/examples/color/colorbar_histogram.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index fc4271847b92..1c60ae6e3c9e 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -1,7 +1,7 @@ """ -========================= -Histogram as Colorbar -========================= +===================== +Histogram as colorbar +===================== This example demonstrates how to create a colorbar for an image and add a histogram of the data values alongside it. This is useful for From fdb37d44076e923adf132043ae6627393ab04b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Wed, 28 May 2025 00:59:04 +0000 Subject: [PATCH 20/21] Remove unnecessary subplot adjustment for image positioning in colorbar histogram example --- galleries/examples/color/colorbar_histogram.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index 1c60ae6e3c9e..ecad38200b40 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -34,9 +34,6 @@ origin='lower', extent=[-3, 3, -3, 3], norm=norm) -# Adjust image position to allow space -plt.subplots_adjust(right=0.78, top=0.92, bottom=0.08) - # === Inset Histogram – Positioning adjusted === cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height From 31cf67d02ca078a41818b110c7dd22bc1502808b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=ADvia=20Lutz?= <108961867+livlutz@users.noreply.github.com> Date: Fri, 30 May 2025 00:25:56 +0000 Subject: [PATCH 21/21] Remove trailing whitespace + adding new layout as constrained --- galleries/examples/color/colorbar_histogram.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/galleries/examples/color/colorbar_histogram.py b/galleries/examples/color/colorbar_histogram.py index ecad38200b40..3c6cc6ef4dd4 100644 --- a/galleries/examples/color/colorbar_histogram.py +++ b/galleries/examples/color/colorbar_histogram.py @@ -29,7 +29,7 @@ norm = mcolors.BoundaryNorm(bins, cmap.N) # === Main Plot === -fig, ax = plt.subplots() +fig, ax = plt.subplots(layout="constrained") im = ax.imshow(Z, cmap=cmap, origin='lower', extent=[-3, 3, -3, 3], norm=norm) @@ -51,7 +51,7 @@ # === Axis labels === cax.set_xlabel('Count', labelpad=10) -cax.set_ylabel('Value', labelpad=6) +cax.set_ylabel('Value', labelpad=6) # === Ticks === cax.set_yticks(bins)