Clear Filters
Clear Filters

How to change data stacking order for a single series in bubble charts?

3 views (last 30 days)
I have a single but large series of xyz data that I want to plot as a bubble chart with bubble colors related to the bubble size values.
However, the plot is not easily readable because the most intense bubbles are hidden by some lower intensity bubbles. How can I change the stacking of the bubbles as a function of bubble size (to plot the most intense bubbles on top of the lowest ones).
Here is a sample code:
figure ;
N = 1e5 ;
x = randn(N,1) ;
y = rand(N,1) ;
z = randi(1e6,[N,1]) ;
[~,edge,idx] = histcounts(z) ;
colors = interp1([min(edge);max(edge)],[1 1 1 ; 0.5 0.5 0.5],edge,"linear","extrap") ;
c = colors(idx,:) ;
figure ;
colormap(c) ;
bubblechart(x,y,z,1:numel(z)) ;
which produces the following output:

Accepted Answer

Star Strider
Star Strider on 21 Oct 2023
MATLAB plotting functions generally plot in the order specified in the pllotting command, and later objects are plotted ‘on top of’ (for lack of a better description) the objects plotted prior to them. Changing the plotting order could be an option.
Otherwise, setting MarkerEdgeAlpha and MarkerFaceAlpha to change their transparencies could work.
  2 Comments
phenan08
phenan08 on 1 Nov 2023
Tank you Star Strider.
As there is only one series to plot, there is no way to change the plotting order of the different series.
However, it is possible to sort the series by ascending Z values.
Without sorting, the representation is not very readable:
N = 500 ;
X = rand(N,1) ;
Y = rand(N,1) ;
Z = rand(N,1) ;
bs = [1 30] ;
figure ;
bubblechart(X,Y,Z,sqrt(gray(N)),"MarkerFaceAlpha",1) ;
bubblesize(bs) ;
But once the data are sorted, it is much better.
[Z_sorted,idx_sort] = sort(Z,"ascend") ;
X_sorted = X(idx_sort) ;
Y_sorted = Y(idx_sort) ;
figure ;
bubblechart(X_sorted,Y_sorted,Z_sorted,sqrt(gray(N)),"MarkerFaceAlpha",1) ;
bubblesize(bs) ;

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!