我需要控制用MATLAB中的scatterhist
命令生成的图中标记的透明度。
以下文章有助于处理直方图的颜色:Controlling scatterhist bar colors。
答案 0 :(得分:3)
tl; dr :
在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。
% 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. Access Property Values在MATLAB
中
2. Plot markers transparency and color gradient
答案 1 :(得分:0)