0

I need to control the transparency of the markers in the figure produced with the scatterhist command in MATLAB.

fig1

The following post is helpful in handling the color of the histograms: Controlling scatterhist bar colors.

  1. How can the transparency of the markers be modified?
  2. How can I add a contour plot on top of the markers?
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Oliver Amundsen
  • 1,491
  • 2
  • 21
  • 40
  • 1
    Could you please clarify what information the contour is supposed to contain? Point density? – Dev-iL Oct 30 '19 at 17:59

2 Answers2

3

tl;dr: In MATLAB R2019a,
scatterhist() can do contours but it is difficult (yet possible) to add marker transparency, and
scatterhistogram() can easily do transparency but contours are difficult.

See the third option below using alpha(), scatter(), and histogram() which builds this from scratch.


% MATLAB R2019a
n = 250;                % Number of Points
X = exprnd(3,n,1);
Y = gamrnd(9,1/3,n,1);  

Using scatterhistogram():

You can adjust the marker transparency with the MarkerAlpha Property.

Plot using scatterhistogram with marker transparency but no contours.

T = table(X,Y);
figure
s = scatterhistogram(T,'X','Y',...
    'HistogramDisplayStyle','smooth',...
    'LineStyle','-')
s.MarkerAlpha = 0.5;                    %  adjust transparency

The documentation demonstrates variations of this technique.

Notice that scatterhistogram() cannot be used with hold on either before or after, which prevents using this solution from MATLAB Central.

% This will give an error in R2019a
figure
s = scatterhistogram(T,'X','Y','HistogramDisplayStyle','smooth','LineStyle','-')
hold on
[m,c] = hist3([X', Y']);            % [m,c] = hist3([X(:), Y(:)]);
contour(c{1},c{2},m)

Using scatterhist():

If you name s = scatterhist(X,Y), then s(1) is the scatter plot, s(2) & s(3) are the histograms. This allows you to change properties. Notice that s(1).Children.MarkerFaceColor = 'b' works fine but there is no MarkerAlpha or MarkerFaceAlpha property (you'll get an error telling you so).

But, contours are possible. I think transparency is possible to based on this comment from @Dev-iL, but I haven't figured it out yet.

Plot using scatterhist with contours but no marker transparency.

figure
s = scatterhist(X,Y,'Direction','out')
s(1).Children.Marker = '.'
hold on
[m,c] = hist3([X(:), Y(:)]);
ch = contour(c{1},c{2},m)

Build it from scratch:
Obviously the entire thing can be manually constructed from scratch (but that's not appealing).

Using the alpha() command gets it done.

Scatterplot with histograms, transparency, and contours.

figure1 = figure;

% Create axes
axes1 = axes('Tag','scatter','Parent',figure1,...
    'Position',[0.35 0.35 0.55 0.55]);
hold(axes1,'on');

% Create plot
s = scatter(X,Y,'Parent',axes1,'MarkerFaceColor','r','Marker','o');

ylabel('Y');
xlabel('X');
box(axes1,'on');
% Create axes
axes2 = axes('Tag','yhist','Parent',figure1,...
    'Position',[0.0325806451612903 0.35 0.217016129032258 0.55]);
axis off
hold(axes2,'on');

% Create histogram
hx = histogram(X,'Parent',axes2,'FaceAlpha',1,'FaceColor','r',...
    'Normalization','pdf',...
    'BinMethod','auto');
view(axes2,[270 90]);
box(axes2,'on');

% Create axes
axes3 = axes('Tag','xhist','Parent',figure1,...
    'Position',[0.35 0.0493865030674847 0.55 0.186679572132827]);
axis off
hold(axes3,'on');

% Create histogram
hy = histogram(Y,'Parent',axes3,'FaceAlpha',1,'FaceColor','r',...
    'Normalization','pdf',...
    'BinMethod','auto');
box(axes3,'on');
axis(axes3,'ij');

[m,c] = hist3([X(:), Y(:)]);
contour(axes1,c{1},c{2},m)

alphaVal = 0.3;
alpha(s,0.5)            % Set Transparency
alpha(hx,0.5)
alpha(hy,0.5)

References:
1. Access Property Values in MATLAB
2. Plot markers transparency and color gradient

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
0

For Matlab versions prior to 2018, scatterhistogram isn't available. And so, I found an alternative easy way to accomplish that markers have transparency:

figure
scatterhist(X,Y,'Kernel','on'); hold on
hdl = get(gca,'children');
set(hdl,'MarkerEdgeColor','none')
scatter(hdl.XData,hdl.YData,50,'MarkerFaceColor','r',...
'MarkerEdgeColor','none','MarkerFaceAlpha',0.2)

This works nicely.

enter image description here

Oliver Amundsen
  • 1,491
  • 2
  • 21
  • 40