Stacked plot graphics, trying to color multiple lines on one y-axis.

5 views (last 30 days)
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed)
TT2 = retime(TT,'hourly','linear')
linespec = ["Color", "black"];
vars = {["Temp", "Pressure", "WindSpeed"]};
t = stackedplot(TT2, vars, linespec{:});
I have a stacked plot of timeseries data, and i am showing it all on one y-axis. Is there a way to color each line differently to make them more pronounced? It seems relatively simple for regular plots, but I have not yet found a way to do it on stacked plots.

Accepted Answer

dpb on 22 Sep 2022
There's an array in the <'StackedLineProperties'> gotten to by the links in the "See Also" section of the documentation.

Sign in to comment.

More Answers (1)

dpb on 26 Sep 2022
OK, had just a few more minutes -- the real answer is that there's a 'LineProperties' handle under the stacked plot object handle that does have the various line properties. If you plot yours as is, it will return a single row, but by trial, you can set the .Color' array to a Nx3 array of RGB colors where N is the number of lines on the axes.
>> t = stackedplot(TT2, vars,'k'); % draw 'em all black
> > t.LineProperties.Color % show the one color
ans =
0 0 0
>> C=[ % define a color array (default)
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250];
>> t.LineProperties.Color=C; % try to change...
and, indeed, the line colors do change for all three.
Also, another way at it is that the axes is a child of the hidden property 'NodeChildren'
>> t.NodeChildren
ans =
2×1 graphics array:
Legend (Temp, Pressure, WindSpeed)
>> hAx=t.NodeChildren(2)
hAx =
Axes with properties:
XLim: [Dec 18, 2015, 08:00 Dec 18, 2015, 13:00]
YLim: [6.6885 43.8151]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.1362 0.1100 0.7688 0.8150]
Units: 'normalized'
Show all properties
>> hAx.Children
ans =
3×1 Line array:
Line (WindSpeed)
Line (Pressure)
Line (Temp)
>> hAx.Children(1)
ans =
Line (WindSpeed) with properties:
Color: [0.9290 0.6940 0.1250]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [18-Dec-2015 08:00:00 18-Dec-2015 09:00:00 18-Dec-2015 10:00:00 18-Dec-2015 11:00:00 18-Dec-2015 12:00:00 18-Dec-2015 13:00:00]
YData: [13.5770 10.1327 6.6885 6.8783 7.2785 7.6788]
ZData: [1×0 double]
Show all properties
This would get you access to a given line by itself directly which could be useful on occasion. Again, though, why TMW has to hide or obfuscate stuff instead of keeping the familiar axes properties/children is beyond ken...just introduce new complexities for the fun of it it seems.


Find more on Line Plots 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!