Controlling scatterhist Marker Transparency

走远了吗. 提交于 2019-12-25 01:16:49

问题


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

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?

回答1:


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.

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.

% 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.

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.

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



来源:https://stackoverflow.com/questions/58628007/controlling-scatterhist-marker-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!