Ideas
Follow


Emma Farnan

Add alpha capability to "Line" class

Emma Farnan on 16 Nov 2023
Latest activity Reply by Emma Farnan on 28 Nov 2023

I think it would be a really great feature to be able to add an Alpha property to the basic "Line" class in MATLAB plots. I know that I have previously had to resort to using Patch to be able to plot semitransparent lines, but there are also so many other functions that rely on the "Line" class.
For example, if you want to make a scatter plot from a table with things specified into groups, you can use ScatterHistogram or gscatter but since gscatter uses the Line class, you can't adjust the marker transparency. So if you don't want the histograms, you are stuck with manually separating it and using scatter with hold on.
Walter Roberson
Walter Roberson on 16 Nov 2023
A poorly documented point is that "chart line" objects accept RGBA color specifications. For example,
plot(rand(1,20),'Color', [.9 .3 .5 .2])
plots a line with an alpha value of 0.2
However... if for any reason the element is saved, then the saved version will not store the alpha component. In particular, savefig() will not store the alpha component; and if you try to do graphics in a parallel worker and send the graphics back, then what is received will (probably) not have the alpha component.
Emma Farnan
Emma Farnan on 17 Nov 2023
I actually had tried that too, because I have seen that RGBA encoding before but it unfortunately is not accepted for typical "line" objects. That is additionally frustrating that the RGBA likely wouldn't work for any saved version though because I almost always incorporate the save functionality into my scripts directly using saveas and it would be frustrating for the figures to come out differently than they displayed.
Walter Roberson
Walter Roberson on 17 Nov 2023
When I do
y = rand(1,20);
plot(y, 'Color', [.9 .3 .5 .5])
saveas(gca, '/tmp/test.png')
then the png does reflect the alpha contribution.
Walter Roberson
Walter Roberson on 17 Nov 2023
Could you give an example of a "typical" line object for this purpose?
Emma Farnan
Emma Farnan on 28 Nov 2023
Sorry about the long reply, I was on holiday.
But I think the issue I am referring to is related to the markers of a Line object. I noticed it initially while using gscatter, but it can be replicated with plot as well.
% Load in the sample data used in gscatter
load carsmall
% Replicate the first example plot in gscatter documentation
figure; g = gscatter(Displacement,Horsepower,Model_Year);
% h is a 3x1 line array with 'LineStyle' set to 'none' for all 3 groups of scatter points.
% - Trying to set one of the Line to have an alpha property does nothing
% unless you change LineStyle to something other than 'none' since the
% 'color' property doesn't affect markers.
g(1).Color = [g(1).Color, 0.3]; % Add alpha of 0.3 to color property
% - Changing 'MarkerFaceColor' or 'MarkerEdgeColor' to the RGBA value
% throws the error I was initially referring to in the "Line" class
g(1).MarkerFaceColor = [.9 .3 .5 .5]; % Comment to skip error
The same issue can be replicated using plot by trying to adjust the marker colors there as well.
% using the same plot type that you made initially Walter
y = rand(1,20);
h = plot(y,'o-','LineWidth',2','Color', [.9 .3 .5 .2]);
% Try to adjust the marker color will again throw the same error
h.MarkerFaceColor = [.9 .3 .5 .2]; % Will cause error
The only way that I know to overcome this is to use scatter because it has adjustable properties 'MarkerEdgeAlpha' and 'MarkerFaceAlpha'.
Adam Danz
Adam Danz on 16 Nov 2023
The alpha channel will also currently not work in the live editor.
Adam Danz
Adam Danz on 16 Nov 2023
Thanks for sharing your idea here!