tl;博士:
在 MATLAB R2019a 中,
scatterhist() 可以做轮廓但很难(possible)添加标记透明度,而
scatterhistogram() 可以很容易地做透明度但轮廓很难。
请参阅下面使用alpha()、scatter() 和histogram() 的第三个选项,它是从头开始构建的。
% MATLAB R2019a
n = 250; % Number of Points
X = exprnd(3,n,1);
Y = gamrnd(9,1/3,n,1);
使用scatterhistogram():
您可以使用MarkerAlpha 属性调整标记透明度。
T = table(X,Y);
figure
s = scatterhistogram(T,'X','Y',...
'HistogramDisplayStyle','smooth',...
'LineStyle','-')
s.MarkerAlpha = 0.5; % adjust transparency
文档演示了这种技术的变体。
请注意,scatterhistogram() 不能在之前或之后与 hold on 一起使用,这会阻止使用 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)
使用scatterhist():
如果您命名s = scatterhist(X,Y),那么s(1) 是散点图,s(2) 和s(3) 是直方图。这允许您更改属性。请注意s(1).Children.MarkerFaceColor = 'b' 工作正常,但没有MarkerAlpha 或MarkerFaceAlpha 属性(您会收到一个错误告诉您)。
但是,轮廓是可能的。我认为透明度可以基于@this comment 和@Dev-iL,但我还没有弄清楚。
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)
从头开始构建:
显然,整个东西都可以从头开始手动构建(但这并不吸引人)。
使用alpha() 命令完成。
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)
参考资料:
1. MATLAB 中的Access Property Values
2.Plot markers transparency and color gradient