Plotting scalebar in scatter plot

Hi all,

I wonder how one could design a “scalebar” function for the size of the circles in scatter plot, next to the plot ?

scatter(x, y, msizes)

In the same philosophy as colorbar but for sizes.

For example something like below:

Thank you for suggestions,

David

Hello David,

Here is a draft for such a function, with a use case:

function scatterbar(sp,x,text)
    n = size(x,"*");
    siz = linspace(min(sp.mark_size),max(sp.mark_size),n);
    drawlater
    cb = colorbar(0,1);
    cb.axes_visible = "off";
    cb.margins=[-0.1 0.4 0 0]
    delete(cb.children);
    copy(sp,cb);
    scbs = cb.children;
    y = linspace(1/2/n,1-1/n,n);
    scbs.data = [0.75*ones(1,n); y]';
    scbs.mark_size_unit = sp.mark_size_unit;
    scbs.mark_size = siz;
    sca(cb);
    str = sprintf("%5.2f\n",x(:));
    str(isnan(x)) = " ";
    hstr = (xstring(1.25*ones(n,1),y,str)).children;
    hstr.text_box_mode = "centered";
    if exists("text","local")
        xstring(1,1-1/3/n,text).text_box_mode = "centered";
    end
    drawnow
endfunction

n = 150;
x = rand(1,n);
y = rand(1,n);
mAreas = 10.^grand(1,n,"unf",1,2.8);

clf
p = scatter(x, y, mAreas, "yel", "fill", "markerEdgeColor", "red");
scatterbar(p,[20 50 %nan 200 500],"Unit:")

scatter

3 Likes