0

我需要控制用scatterhistMATLAB 中的命令生成的图中标记的透明度。

图。1

以下帖子有助于处理直方图的颜色:控制散点图条颜色

  1. 如何修改标记的透明度?
  2. 如何在标记顶部添加等高线图?
4

2 回答 2

3

tl;dr:在 MATLAB R2019a 中,
scatterhist()可以做轮廓,但很难(但可能)添加标记透明度,并且
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 onbefore 或 after 一起使用,这会阻止从 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'工作正常,但没有MarkerAlphaorMarkerFaceAlpha属性(你会得到一个错误告诉你)。

但是,轮廓是可能的。我认为基于@ Dev-iL 的评论可以实现透明度,但我还没有弄清楚。

使用具有轮廓但没有标记透明度的 scatterhist 进行绘图。

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 中访问属性值
2.绘制标记透明度和颜色渐变

于 2019-10-30T16:42:51.303 回答
0

对于 2018 年之前的 Matlab 版本,散点图不可用。因此,我找到了另一种简单的方法来实现标记具有透明度:

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)

这很好用。

在此处输入图像描述

于 2021-02-11T16:36:08.910 回答