控制散点图标记的透明度 [英] Controlling scatterhist Marker Transparency

查看:857
本文介绍了控制散点图标记的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要控制在MATLAB中使用scatterhist命令生成的图中标记的透明度.

以下文章有助于处理直方图的颜色:控制散点图颜色.

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

解决方案

tl; dr : 在MATLAB R2019a中,
scatterhist() 可以绘制轮廓,但很困难(但 scatterhistogram() 可以轻松实现透明度,但轮廓很难.

使用 alpha() scatter() scatterhistogram() :

您可以使用MarkerAlpha属性调整标记的透明度.

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

文档演示了此技术的变体.

请注意 scatterhistogram() 不能与之前或之后,这会阻止使用> scatterhist() > :

如果您命名s = scatterhist(X,Y),则s(1)是散点图,s(2)& s(3)是直方图.这使您可以更改属性.请注意,s(1).Children.MarkerFaceColor = 'b'可以正常工作,但是没有MarkerAlphaMarkerFaceAlpha属性(您会得到一个错误提示).

但是,可以轮廓.我认为透明度可以基于@ 此评论 "https://stackoverflow.com/users/3372061/dev-il">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
2. 绘图标记的透明度和颜色渐变

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?

解决方案

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

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

这篇关于控制散点图标记的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆