Multiple Lines in a Stacked Plot

I'd like to have 3 different vectors plotted onto 2 graphs within a stacked plot. For example, I wish to plot vectors a and b in the first graph of the stacked plot and variable c on the second graph, with the x-axis of the stacked plot being another vector, t. How would I be able to do that?

 Risposta accettata

Dyuman Joshi
Dyuman Joshi il 21 Nov 2023
Spostato: Dyuman Joshi il 21 Nov 2023
If you are working with R2018b or a later version, check out stackedplot
%Random data
n = 10;
t = (1:n).';
a = zeros(n,1);
b = ones(n,1);
c = rand(n,1);
data = table(t, a, b, c)
data = 10×4 table
t a b c __ _ _ ________ 1 0 1 0.89507 2 0 1 0.099055 3 0 1 0.70476 4 0 1 0.65991 5 0 1 0.78316 6 0 1 0.80992 7 0 1 0.32504 8 0 1 0.10411 9 0 1 0.6188 10 0 1 0.34006
vars = {["a" "b"], "c"};
stackedplot(data, vars, "XVariable", "t")

4 Commenti

It worked! An additional question: how can I change the visual properties (say, color) of those lines on the first lot individually? I can only seem to change the color of both a and b.
You can access the properties of the stackedplot by getting the StackedLineChart Properties object and then modifying as per need/requirement/liking -
%Random data
n = 10;
t = (1:n).';
a = zeros(n,1);
b = ones(n,1);
c = rand(n,1);
data = table(t, a, b, c)
data = 10×4 table
t a b c __ _ _ _______ 1 0 1 0.92642 2 0 1 0.16481 3 0 1 0.16602 4 0 1 0.72322 5 0 1 0.89256 6 0 1 0.54806 7 0 1 0.49016 8 0 1 0.643 9 0 1 0.66422 10 0 1 0.29326
vars = {["a" "b"], "c"};
s = stackedplot(data, vars, "XVariable", "t");
arr = s.LineProperties
arr =
2×1 StackedLineProperties array with properties: Color MarkerFaceColor MarkerEdgeColor LineStyle LineWidth Marker MarkerSize PlotType
%Get the color of the first plot
col = arr(1).Color
col = 2×3
0 0.4470 0.7410 0.8500 0.3250 0.0980
%Change the color of the first plot
new_col = hsv(size(col,1))
new_col = 2×3
1 0 0 0 1 1
arr(1).Color = new_col;
Works great, thank you so much!
You are welcome!

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by