How can I make the plot transparent in a gscattter?
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Yair Altman
il 27 Ago 2020
Modificato: Yair Altman
il 5 Ott 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 Commenti
Adam Danz
il 27 Ago 2020
Thanks, Yair! I should have known to check your blog, first!
Sim
il 4 Ott 2022
Adam Danz
il 4 Ott 2022
Yes, see my answer.
Yair Altman
il 5 Ott 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
il 16 Nov 2022
Modificato: Gernot Reichl
il 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
il 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
il 16 Nov 2022
Modificato: Gernot Reichl
il 16 Nov 2022
@Yair Altman Thank you very much for your answers and your great work!
Joseph Mattson
il 13 Dic 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.
Categorie
Scopri di più su Graphics Object Properties in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

