Horizontal colorbars in matplotlib


There may be improvement of default behavior for plotting a QuadMesh object with horizontal colorbars using matplotlib. Here is an image with vertical colorbar:

To change it horizontally, I added the following to the opts of the QuadMesh object:
colorbar_opts={"orientation":"horizontal"}, but got the following

Note that the colorbar orientation indeed changed but the colorbar remained in the same location. The default behavior matplotlib does is to put it under the main figure. From “holoviews/plotting/mpl/element.py” it seems to me that _draw_colorbar function always puts colorbar side by side with the main figure. I was able to achieve what matplotlib does by applying hooks=[hook],where

def hook(plot, element):
    cax = plot.handles['cax']
    ax = plot.handles['axis']
    bbox = ax.get_position()
    l, b, w, h = bbox.x0, bbox.y0, bbox.width, bbox.heighti
    cax.set_position([l, 0, w, 0.05*h])

with result being

I was thinking if this process could be the default behavior when horizontal colorbar is requested. Also, I find that styling the colorbar usually involves working with the underlying plotting mechanism directly, even for small things such as changing color axis tick sizes, color axis label sizes. I had to use hooks for them instead of working with opts. Am I doing it the correct way?

A followup issue. I created two QuadMesh plots each of which has the options hooks=[hook], where hook is the function described above. When I put them into a Layout, they each became the style shown in the second figure. I tried putting a traceback.form_stack() call in the hook function, and saw that hook was correctly invoked for each subplot. So I guess it is some step in forming the Layout plot that overrides the setting? Or in general, how does LayoutPlot handle hooks in its constituents? Thanks.