How to change color of line within multiple ranges of same line

2 次查看(过去 30 天)
Lets say I have the following 1xn matrix:
mat =
2
4
8
6
32
64
28
256
512
1024
2048
4096
How can I plot the line to display specific colors along certain ranges (ie. default blue from 1:3, green from 4:7, white/invisible from 8:10, red 11:12)?
The solutions I have found are for x,y plots but I am not sure how apply these display alterations to a matrix/vector (insofar as that is concerned, I do not really understand how Matlab creates x,y plots from one dimensional vectors).
Thanks for any help.

采纳的回答

Star Strider
Star Strider 2015-12-6
This looks a bit strange, but it does what you want:
mat = [ 2
4
8
16
32
64
128
256
512
1024
2048
4096];
x = [1:size(mat,1)];
rmat = reshape(mat, 3, 4);
rx = reshape(x, 3, 4);
c = [0 0 1; 0 1 0; 1 1 1; 1 0 0];
figure(1)
semilogy(rx(:,1), rmat(:,1), 'Color',c(1,:))
hold on
for k1 = 2:size(rmat,2)
plot(rx(:,k1), rmat(:,k1), 'Color',c(k1,:))
end
hold off
grid
I did a semilogy plot so I could verify that it worked.
  15 个评论
JZ
JZ 2015-12-12
I realize which code is best suited for my task, however I am trying to more clearly understand the differences between the code that plots the evenly spaced matrix:
mat = [ 2
4
8
16
32
64
128
256
512
1024
2048
4096];
x = [1:size(mat,1)];
ix_mtx = [1 3; 3 9; 11 12];
cv = ['-r'; '-g'; '-m'];
figure(1)
plot(x(ix_mtx(1,:)), mat(ix_mtx(1,:)), cv(1,:))
hold on
for k1 = 2:3
plot(x(ix_mtx(k1,:)), mat(ix_mtx(k1,:)), cv(k1,:))
end
and the code code that properly plots my data:
mat = perfectVel;
x = 1:length(mat);
ix_mtx = [1 16; 16 17; 17 32];
cv = ['-r'; '-g'; '-m'];
figure(1)
plot([x(ix_mtx(1,1):ix_mtx(1,2))], [mat(ix_mtx(1,1):ix_mtx(1,2))], cv(1,:))
hold on
for k1 = 2:3
plot([x(ix_mtx(k1,1):ix_mtx(k1,2))], [mat(ix_mtx(k1,1):ix_mtx(k1,2))], cv(k1,:))
end
Why is it that I get the following undesired result (image below) when I use the code meant for the evenly spaced matrix? In other words, how is it that an evenly spaced matrix of values allows for the simplified code; why doesn't the same code work for an unpatterned matrix (since it is also n-by-1).
The only thing that I changed in the code for the plot below is the matrix data and the color ranges. I can't see why the code should be different.
Star Strider
Star Strider 2015-12-12
It’s because the latest code was written to be more general. The early (particularly first) code was written only for that specific example. The latest code is correct, but you have to make the appropriate changes to it (specifically ‘ix_mtx’) to use it with other data.
You have 32 data in your actual data. To use my original code with it, you would have to do a reshape call that divided it into either a (16x2) or (8x4) matrix (or the obvious transposes of those). The data would not divide as you want them to here.
Not all code is robust, or intended to be. Frequently, here on MATLAB Answers, we write code to Answer only a specific Question. If the actual application is significantly different from the ‘example’ Question, as it was here, the original code will quite likely not work in the code's eventually intended application.
The only reasonably robust code you will find contributed by many of the user community is on the File Exchange. It is robust because it was designed to be, and not as a one-off, as most MATLAB Answers code are.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by