Saving figures (with saveas or with savefig) does not save the alpha value that I've set for the line objects
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Alex Ogren
le 13 Juin 2020
Commenté : Steven Brossi
le 5 Fév 2021
I'm trying to save matlab figure files where I've set the transparency of the plot lines using the fourth element in the color vector.
p1 = line(x,y,'Color',[0 0 0 .1],'LineStyle','-','Marker','.');
saveas(curr_fig1,'myfig1.fig');
However, when I open the figure (either by double clicking it in file explorer, or by using the open or openfig commands) the plots no longer have any transparency. Does anyone know a way around this? Here is a full snippet of code that can be run independently to highlight the problem.
clear; close all;
curr_fig1 = figure();
x = 0:.1:2;
y = sin(x);
figure(1);
p1 = line(x,y,'Color',[0 0 0 .1],'LineStyle','-','Marker','.');
saveas(curr_fig1,'myfig1.fig');
openfig('myfig1.fig');
savefig(curr_fig1,'myfig2.fig');
openfig('myfig2.fig');
UPDATE:
It seems that the specific issue is that the Line.Edge.ColorData is not preserved during the saving process.
To highlight what I mean by this, try running the code below.
clear; close all;
curr_fig1 = figure();
x = 0:.1:2;
y = sin(x);
f1 = figure(1); % figure f1
p1 = line(x,y,'Color',[0 0 0 .1],'LineStyle','-','Marker','.'); % line p1 is a child of axes a1, which is a child of f1
a1 = f1.Children(1); % axes a1 are child of figure f1 (this variable is not used, but can be examined)
saveas(curr_fig1,'myfig1.fig','m');
f2 = openfig('myfig1.fig'); % figure f2
a2 = f2.Children(1); % axes a2 are the axes of figure f2
p2 = a2.Children(1); % Line p2 is a child of axes a2
% Nominally, we would want the Edge.ColorData to be the same for the object that
% I saved and the object that I opened. However, this is not the case.
disp('The Edge.ColorData for the original line p1 is:')
display(p1.Edge.ColorData)
disp('')
disp('The Edge.ColorData for the saved/opened line p2 is:')
display(p2.Edge.ColorData)
% We can further notice that when I save and reopen, the Edge.ColorType changes
% from 'truecoloralpha' to 'truecolor', which may be part of the problem
disp('The Edge.ColorData for the original line p1 is:')
display(p1.Edge.ColorType)
disp('')
disp('The Edge.ColorData for the saved/opened line p2 is:')
display(p2.Edge.ColorType)
I'm trying to find a way to force MATLAB to recognize the fact that I've changed these properties, and save the values that I've assigned to them.
CONTEXT:
The reason that I'm using transparency is to see when there is a high density of data in certain regions, so the interaction of transparency between different lines is important. Here's an example of a plot that I create. It is a plot that contains about 2000 curves in a confined space. Before saving, the transparency works and I am able to see where the curves are dense. After saving and reopening, the figure is just a mess of fully opaque pixels.
To create the following image, I save as pdf:
To create the following image, I save as fig, reopen as fig, and save as pdf:
0 commentaires
Réponse acceptée
Ameer Hamza
le 13 Juin 2020
Setting transparency using the 4th element is not documented. Also, the line object does not seem to save. If you check
p1.Color
You will see that it only has 3 elements. Therefore, I guess the transparency value is not properly supported yet.
You can have a look at my answer here: https://www.mathworks.com/matlabcentral/answers/401011-exact-value-of-facecolor-in-histogram to see how you can set rgba color using just rgb. Basically, you can use rgba to rgb conversion tool (such as this: http://marcodiiga.github.io/rgba-to-rgb-conversion) to get an rgb equivalent for the given color rgba value. Set the background color to white when doing the conversion.
5 commentaires
Steven Brossi
le 5 Fév 2021
Thanks! That saved me some time.
Sad, it isn't supported, yet (R2020b) because using transparency is often very useful.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!