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

21 次查看(过去 30 天)
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"]};
figure(1)
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.

采纳的回答

dpb
dpb 2022-9-22
There's an array in the <'StackedLineProperties'> gotten to by the links in the "See Also" section of the documentation.
  7 个评论
dpb
dpb 2022-9-26
I had forgotten could do that with stackedplot, sorry.
Looks like they overlooked a way to pass an array of colors; you can get the default sequence of color order if you just remove the linespec argument in your call.
I didn't test it, but you should be able to get any desired sequence of colors by defining a new colororder for the axes.
I'd have to dig further to see if there's some other syntax I'm missing to allow the array of colors in the call when grouping, but at first blush looks like that feature isn't impemented; I had assumed with a stacked plot, there would be an array of lines regardless of the axes; that's shortsighted on TMW's part to not have done -- I simply hate it when they build stuff like this that isn't symmetric.

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2022-9-26
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)
Axes
>> 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.

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by