@@ -493,6 +493,35 @@ def example_plot(ax, fontsize=12, hide_labels=False):
493
493
ax .imshow (arr )
494
494
fig .suptitle ("fixed-aspect plots, layout='compressed'" )
495
495
496
+ # %%
497
+ # Compressed layout will also attempt to size colorbars to match the size of the
498
+ # fixed-aspect-ratio parent Axes as the figure is resized or the aspect ratio changes.
499
+ # In the following figure, the colorbar is taller than its parent Axes:
500
+
501
+ fig , ax = plt .subplots (layout = 'constrained' , figsize = (3 , 3 ))
502
+ pcm = ax .imshow (np .random .randn (10 , 10 ), cmap = 'viridis' )
503
+ ax .set_title ("Colorbar with layout='constrained'" , fontsize = 'medium' )
504
+ fig .colorbar (pcm , ax = ax )
505
+
506
+ # %%
507
+ # Compressed layout ensures that the height of the colorbar matches the height
508
+ # of its parent Axes, maintaining a consistent appearance:
509
+
510
+ fig , ax = plt .subplots (layout = 'compressed' , figsize = (3 , 3 ))
511
+ pcm = ax .imshow (np .random .randn (10 , 10 ), cmap = 'viridis' )
512
+ ax .set_title ("Colorbar with layout='compressed'" , fontsize = 'medium' )
513
+ fig .colorbar (pcm , ax = ax )
514
+
515
+ # %%
516
+ # If the Axes is zoomed in or out, or the figure is resized, the colorbar will
517
+ # dynamically resize to match the parent Axes. Whether this behavior is desired
518
+ # depends on the specific application:
519
+
520
+ fig , ax = plt .subplots (layout = 'compressed' , figsize = (3 , 3 ))
521
+ pcm = ax .imshow (np .random .randn (10 , 10 ), cmap = 'viridis' )
522
+ ax .set_ylim ([4 , 8 ])
523
+ ax .set_title ("Layout='compressed' with zoom" , fontsize = 'medium' )
524
+ fig .colorbar (pcm , ax = ax )
496
525
497
526
# %%
498
527
# Manually turning off *constrained layout*
0 commit comments