Change the line color in subplot
Show older comments
I am using a code to complete an anlyses and then produce 2 graphs using subplot. I have now set it up so it will plot a number of trials onto the same figure. However I would like to change the color or format of the lines so i can tell them apart. I see that this is ealiy done with plot(x,y,'r') however i do not know how i can code this when using a subplot.
Thanks for any help
Answers (2)
C.J. Harris
on 16 Jul 2012
An easier way might be to call 'hold all' after each subplot definition, and then just plot your values as normal. For example:
subplot(2,1,1)
hold all
plot(x1,y1)
plot(x2,y2)
plot(x3,y3)
hold off
subplot(2,1,2)
hold all
plot(x1,y1)
plot(x2,y2)
plot(x3,y3)
hold off
A subplot is only a more powerful axes command, which allows a smarter setting of the position. You still have to create the diagram by plot or line, therefore I do not understand the question.
Please add the relevant part of your code by editing the question. Thanks.
Some guessing:
Axes1H = subplot(1,2,1);
Axes2H = subplot(1,2,2);
Line1H = plot(1:10, rand(1, 10), 'Parent', Axes1H);
Line2H = plot(3:12, rand(1, 10), 'Parent', Axes2H);
set(Line1H, 'LineColor', [1, 1, 0.5]);
set(Line2H, 'LineColor', [0.5, 1, 0.5]);
4 Comments
Rina Blomberg
on 8 Nov 2021
Hi Jan
Not sure if you want to follow through on answers to this question 10 years later but I'd really like to know how to change the line color from Green to Blue (for example) for all the following subplots within this main Figure (representing ERP curves from 129 channels - a matlab figure output from the application "EEGlab") without having to click on the properties for each individual green line in the GUI. I haven't been successful with my own attempts to work this out.

Let's take a look at a sample figure with some lines.
x = 0:30:360;
axis([0 360 -1 1])
plot(x, sind(x), 'g-o', x, cosd(x), 'g:s')
Now if I'd planned ahead I would have called plot with an output argument so I had the handles of the lines. But I'm not sure that's an option with EEGlab so I'll show how to get the handles using findobj. Depending on how EEGlab created the handles (whether the handles are hidden or not) you may need to use findall instead.
allLinesInAxes = findobj(gca, 'Type', 'line') % or to get just the green lines
allGreenLinesInAxes = findobj(gca, 'Type', 'line', 'Color', 'g')
Now you can set the color of the lines. I'll recreate the figure here and create a new variable with the line handles because the editor in MATLAB Answers doesn't always do what I'd like it to do with respect to when it "snapshots" the figure. [I've filed an enhancement request for a way to manually tell the Editor "take a snapshot" for use cases like this one where I want to show before-and-after.]
figure
axis([0 360 -1 1])
plot(x, sind(x), 'g-o', x, cosd(x), 'g:s')
allGreenLinesInNewAxes = findobj(gca, 'Type', 'line', 'Color', 'g');
set(allGreenLinesInNewAxes, 'Color', 'b')
Now the lines are blue. The other properties (line style, marker) of the lines haven't changed.
Rina Blomberg
on 8 Nov 2021
Edited: Rina Blomberg
on 8 Nov 2021
Thank you for such a quick reply. I list below my steps and hope this helps to isolate my problem.
- I open the figure in matlab (via eeglab).
- In the command line I tried both findall and findobj and got identical results:
>> allLinesInAxes = findall(gca, 'Type', 'line')
allLinesInAxes =
4×1 graphics array:
Line
Line
Line
Line
>> allLinesInAxes = findobj(gca, 'Type', 'line')
allLinesInAxes =
4×1 graphics array:
Line
Line
Line
Line
- The 4x1 array corresponds to only one subplot (the "Cz" channel), which, when I apply the remainder of your code: set(allGreenLinesInNewAxes, 'Color', 'b') to the output above it successfully updates the green line in that subplot to blue (see attached screenshot)
Have I done something wrong or is there something more I need to do in order to "find" all 129 subplots and peform the same procedure on all?

Steven Lord
on 8 Nov 2021
When you call findobj with an axes handle as the first input, you find only objects in that axes and its descendants.
If you were to call findobj with a figure handle as the first input, you'd find only objects in that figure and its descendants.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!
