I have this figure here, and I need to change the line style of the 2 horizontal images in (b) online. The command :
s.LineProperties(2).Color = 'r'
changes all 3 lines, and I am not sure how to change only a single one.
Thanks!

2 commentaires

KALYAN ACHARJYA
KALYAN ACHARJYA le 30 Mai 2021
Please share the code for quick fix, if possible
Hugo Fortescue
Hugo Fortescue le 30 Mai 2021
I solved it crudely using a tiledlayout with spacing set to none.

Connectez-vous pour commenter.

 Réponse acceptée

Adam Danz
Adam Danz le 31 Mai 2021
Modifié(e) : Adam Danz le 2 Juin 2021
> "I have this figure here, and I need to change the line style of the 2 horizontal images in (b) online. The command s.LineProperties(2).Color='--r' changes all 3 lines, and I am not sure how to change only a single one."
In the original version of your question before it was edited, the color assignment included a linstyle ('--r').
You have to set them separately. These settings will be applied to all lines within the selected axes.
data = sin(linspace(-pi,pi,100)'.*(.5:.5:2.5));
s = stackedplot(data);
s.LineProperties(2).Color = 'r';
s.LineProperties(2).LineStyle = '--';
Working with multiple lines per axes
If there are multiple lines in a pair of stacked axes and you only want to change the color of one line, get the line handles from the axes handles.
% Create table
data = sin(linspace(-pi,pi,100)'.*(.5:.5:3.5));
T = array2table(data,'VariableNames',{'A' 'B' 'C' 'D' 'E' 'F' 'G'});
% Plot 3 lines on the 2nd axes from the top
figure()
Vars = {'A' {'B' 'C' 'D'} 'E' 'F' 'G'};
S = stackedplot(T,Vars,'-k');
S.AxesProperties(2).LegendVisible = 'off';
% Get axes handles
ax = flip(findobj(S.NodeChildren, 'Type','Axes')); % order: top to bottom
% Get line handles in axes #2
lines = flip(ax(2).Children); % order: top to bottom
% Color line #2 in selected axes
drawnow(); pause(.05) % required
lines(2).Color = 'r';

6 commentaires

Hugo Fortescue
Hugo Fortescue le 3 Juin 2021
The original question edited as that mistake was a typo, and not part of the issue at hand. Thank you for your intital responce none the less.
I did not know that indervidual lines had handles aswell, thank you very much for your time.
Adam Danz
Adam Danz le 3 Juin 2021
The NodeChildren property is undocumented and that's how I got the individual axis handles. The line objects are children of the axes handles.
Tristan Deppe
Tristan Deppe le 8 Sep 2022
Any ideas why the line colors all revert to black if I resize the figure window or try and save/print the figure?
Great question, @Tristan Deppe. This is the trouble with using undocumented methods.
A workaround is to merely add the line to the axes after creating the stackedplot. Get "ax" from my answer, then,
hold(ax(2),'on');
plot(ax(2),...,'r-')
% Create table
data = sin(linspace(-pi,pi,100)'.*(.5:.5:3.5));
T = array2table(data,'VariableNames',{'A' 'B' 'C' 'D' 'E' 'F' 'G'});
% Plot 3 lines on the 2nd axes from the top
figure()
Vars = {'A' {'B' 'C' 'D'} 'E' 'F' 'G'};
S = stackedplot(T,Vars,'-k');
S.AxesProperties(2).LegendVisible = 'off';
% Get axes handles
ax = flip(findobj(S.NodeChildren, 'Type','Axes')); % order: top to bottom
hold(ax(2),'on');
plot(ax(2),...,'r-')
% Get line handles in axes #2
lines = flip(ax(2).Children); % order: top to bottom
% Color line #2 in selected axes
drawnow(); pause(.05) % required
lines(2).Color = 'r';
@Adam Danz I didn't understand your instruction, where should put ?
hold(ax(2),'on');
plot(ax(2),...,'r-')
Adam Danz
Adam Danz le 19 Mai 2023
This answer does not show how to add a line to an axes. It shows how to change the color of an existing line.

Connectez-vous pour commenter.

Plus de réponses (2)

Hugo Fortescue
Hugo Fortescue le 2 Juin 2021
Modifié(e) : Hugo Fortescue le 2 Juin 2021

0 votes

I was not able to get the desried results with stackedplot, but have recreated the same format using tiledlayout.
t = tiledlayout(4, 1)
t.TileSpacing = 'none';
t.Padding = 'loose';
a = nexttile;
plot(A, 'k');
set(a, 'XTickLabel',[], 'xtick', [], 'FontSize', 14);
ylabel('(i)','rotation',0,'VerticalAlignment','middle', 'HorizontalAlignment', 'right');
b = nexttile;
plot(B1, 'k');
hold on
plot(B2, '--r');
plot(B3, '--r');
set(b, 'XTickLabel',[], 'xtick', [], 'FontSize', 14);
ylabel('(ii)','rotation',0,'VerticalAlignment','middle', 'HorizontalAlignment', 'right');
c = nexttile;
plot(C, 'k');
set(c, 'XTickLabel',[], 'xtick', [], 'FontSize', 14);
ylabel('(iii)','rotation',0,'VerticalAlignment','middle', 'HorizontalAlignment', 'right');
d = nexttile;
plot(D, 'k');
set(d, 'FontSize', 14);
ylabel('(iv)','rotation',0,'VerticalAlignment','middle', 'HorizontalAlignment', 'right');
xlabel('X Axis Lable');
han2 = axes(fig2,'visible','off');
han2.YLabel.Visible = 'on';
ylab = ylabel(han2,'Y Axis Lable');
set(han2, 'FontSize', 14)
ylab.Position(1) = -0.1;
ylab.Position(2) = 0.5;
It is by no means a perfect solution, but replicates the desired reuslts well.

1 commentaire

Adam Danz
Adam Danz le 2 Juin 2021
Modifié(e) : Adam Danz le 2 Juin 2021
> I was not able to get the desried results with stackedplot
I just noticed that your 2nd axes has 3 lines - they are difficult to see in the embedded image in your question. I've updated my answer to show how to set the color of a single line in stackedaxes that contains multiple lines per axes.

Connectez-vous pour commenter.

Lianne Wolsink
Lianne Wolsink le 16 Déc 2022
Using stackedplot, the color of one line within an axis containing multiple lines can be changed using this code:
a = stackedplot(table,vars);
a.LineProperties(2).Color = [0 0 0
1 0 0]; % changes colors of lines in axis 2 to black for line 1 and red for line 2

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by