How can I make the plot transparent in a gscattter?
Show older comments
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.
Accepted Answer
More Answers (1)
Yair Altman
on 27 Aug 2020
Edited: Yair Altman
on 5 Oct 2022
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 Comments
Adam Danz
on 27 Aug 2020
Thanks, Yair! I should have known to check your blog, first!
Sim
on 4 Oct 2022
Adam Danz
on 4 Oct 2022
Yes, see my answer.
Yair Altman
on 5 Oct 2022
@sim - simply add a call to drawnow before setting the MarkerHandle's properties, and then you'll see the transparency effect (at least in R2022b, there's no guarranty that it will continue working in future releases as well)
Gernot Reichl
on 16 Nov 2022
Edited: Gernot Reichl
on 16 Nov 2022
Hi all.
I try to set the face-color property of a plot as described (in a live script ==> see attachment). But it does not work. Do you have any ideas why? Release: 2022b
x=1:10; y=10*x; hLine=plot(x,y,'o-');
drawnow;
hMarkers = hLine.MarkerHandle;
hMarkers.get;
drawnow;
hMarkers.FaceColorData = uint8(255*[1;0;0;0.3]);
hMarkers.get;
Running the live-script in command line leads to red markers with no transparency.
thank you very much in advance
Yair Altman
on 16 Nov 2022
2 problems with your code:
- you forgot to set the MarkerHandle.FaceColorType to 'truecoloralpha'
- live scripts embedded figures do not show transparency, only standard standalone figures (i.e., run your code in a .m file, not .mlx)

Gernot Reichl
on 16 Nov 2022
Edited: Gernot Reichl
on 16 Nov 2022
@Yair Altman Thank you very much for your answers and your great work!
Joseph Mattson
on 13 Dec 2023
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.
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
