kakearney / contourfcmap-pkg

Create a filled contour plot in Matlab, with better color-to-value clarity
MIT License
17 stars 2 forks source link

Subplot example request #8

Closed briochemc closed 2 years ago

briochemc commented 2 years ago

Do you have a small example where two subplots can share the same (unique) colorbar?

Here is a "modern" example from the docs:

Z1 = peaks;
Z2 = membrane;
tiledlayout(2,1);
nexttile
contourf(Z1)
nexttile
contourf(Z2)
cb = colorbar;
cb.Layout.Tile = 'east';
exportgraphics("subplot_cbar_example.png")

How would one go about to do something similar with contourfcmap?

kakearney commented 2 years ago

The contourfcmap function calls my pcolorbar function to create the psuedo-colorbar. That psuedo-colorbar consists of a (visible) axis with a patch object that mimics a colorbar, as well as an actual (not visible) colorbar that it uses to determine the position. This pairing allows you to reposition the psuedo-colorbar just like a real one:

cmap = jet(10);
clev = linspace(-2,2,11);

[X1,Y1,Z1] = peaks;
Z2 = membrane;
[X2,Y2] = ndgrid(1:size(Z2,1), 1:size(Z2,2));

tiledlayout(2,1);
nexttile
h1 = contourfcmap(X1,Y1,Z1,clev,cmap, 'method', 'calccontour', ...
    'lo', ones(1,3)*0.2, ...
    'hi', ones(1,3)*0.8, ...
    'cbarloc', 'eastoutside'); % choosing 'eastoutside' puts the ticks on the correct side
nexttile
h2 = contourfcmap(X2,Y2,Z2,clev,cmap, 'method', 'calccontour');

h1.cb.cb.Layout.Tile = 'east'; % <- change colorbar to east of both tiles

subplot_cbar_example