How can I make the plot transparent in a gscattter?

조회 수: 89 (최근 30일)
Mel
Mel 2020년 8월 4일
댓글: Joseph Mattson 2023년 12월 13일
I have a huge amount of data which is grouped and displayed in a plot with the help of gscatter. In order to make it more visible I'd like to change the transparency of each point. But functions such as MakeFaceAlpha etc did not work at all.

채택된 답변

Adam Danz
Adam Danz 2020년 8월 4일
편집: Adam Danz 2020년 8월 27일
Setting transparency of markers with gscatter is not possible as of r2020a.
Update: See Yair's method of setting gscatter marker transparency using undocumented features.
Workarounds
You could use scatter() which does support transparency. Here's one way to translate a gscatter syntax to scatter syntax using arrayfun.
% gscatter syntax
gscatter(x,y,g)
% scatter syntax
cla()
hold on % important
uniqueGroups = unique(group);
h = arrayfun(@(g)scatter(x(group==g), y(group==g), 'filled'), uniqueGroups);
set(h, 'MarkerFaceAlpha', 0.6) % set transparency level
% scatter syntax specifying color, marker, and size
colors = 'rb';
markers = '^v';
uniqueGroups = unique(group);
cla()
hold on % important
h = arrayfun(@(g)scatter(x(group==g), y(group==g), ...
100, colors(g), 'filled', 'Marker', markers(g)), uniqueGroups);
set(h, 'MarkerFaceAlpha', 0.4, 'MarkerEdgeColor', 'k', 'MarkerEdgeAlpha', .2) % set transparency level
Alternatively, you could use gscatter without filled markers.
h = gscatter(x,y,g,colors,'os',10);
set(h,'LineWidth',1)
Why transparency in gscatter doesn't work
The scatter function creates scatter objects. Scatter objects have MarkerFaceAlpha and MarkerEdgeAlpha properties that allow you to set transparency levels.
The gscatter function creates line objects. Line objects do not have these properties. Furthermore, an undocumented method of adding transparancy to some graphics objects by adding a 4th element (0:1) to the RGB color definition does not work with gscatter.
  댓글 수: 2
Adam Danz
Adam Danz 2020년 8월 4일
Note: Demo plot created with
load discrim % built-in dataset
x = ratings(:,1);
y = ratings(:,2);
g = group;
Mel
Mel 2020년 8월 5일
Thank you very much! This helps a lot

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Yair Altman
Yair Altman 2020년 8월 27일
편집: Yair Altman 2022년 10월 5일
Nice answer Adam, but not exactly accurate if you are willing to use some undocumented features...
The underlying objects in a gscatter are simple line objects, whose markers can indeed be made to be transparent: http://undocumentedmatlab.com/articles/plot-markers-transparency-and-color-gradient
The trick is to realize that only the markers' Face can be made transparent, not the markers' Edge. By default, gscatter uses empty marker Face and non-empty Edge with a '.' marker; we can change this to a 'o' marker with no edge and a non-empty Face.
Here's a simple usage example:
load carsmall
h = gscatter(Displacement, Horsepower, Model_Year);
set(h(1), 'Marker','o', 'MarkerSize',5, 'MarkerEdgeColor','none', 'MarkerFaceColor','r');
drawnow
set(h(1).MarkerHandle, 'FaceColorType','truecoloralpha', 'FaceColorData',uint8([200;0;0;50]));
% ...and similarly for the other handles h(2),h(3),...
drawnow
  댓글 수: 8
Gernot Reichl
Gernot Reichl 2022년 11월 16일
편집: Gernot Reichl 2022년 11월 16일
@Yair Altman Thank you very much for your answers and your great work!
Joseph Mattson
Joseph Mattson 2023년 12월 13일
Thank you @Yair Altman for the excellent solution. One follow up to @Gernot Reichl (in 2022b anyway): Figures in .mlx scripts will show transparency if you use the "MarkerFaceAlpha" in a standard scatter plot, e.g.
scatter(x, y, 'filled','MarkerFaceAlpha',0.1);
This does not appear to be the case in line plots (which underly the gscatter). To get transparency in your grouped scatter plots in a live script, I think you'll have to use the technique highlighed by @Adam Danz and avoid gscatter altogether.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by