Plot 2 sets of data in different colour

Hi,
I am trying to plot a set of data but in fact is formed by 2 different set of data, the second starting where the first one ends. In order to understand what I mean, imagine I have: a=linspace(1,10); b=linspace(10,20); then I will have something like: c=[a b]; which will give me a line with all the values. Can I make the same line in 2 different colours, I mean, can make c with a colour representing the data in a and another colour for the data in b? Thanks in advance!

Réponses (1)

KSSV
KSSV le 16 Fév 2017
figure
plot(rand(10,1),rand(10,1),'r',rand(20,1),rand(20,1),'b')
% or
figure
plot(rand(10,1),rand(10,1),'r')
hold on
plot(rand(20,1),rand(20,1),'b')

6 commentaires

Auryn_
Auryn_ le 16 Fév 2017
Hi, No, it doesn't work. I need that the second set of data start right from the last point of the first set of data, as in the example I gave above. Thanks!
@Auryn_: Although KSSV's code does not exactly what you need, it is only a small transfer, which you should solve by your own:
a = linspace(1,10);
b = linspace(10,20);
c = [rand(size(a), rand(size(b))];
plot(a, c(1:length(a)), 'r', ...
b, c(length(a)+1:end), 'b');
Or:
figure
plot(a, c(1:length(a)),'r')
hold on
plot(b, c(length(a)+1:end),'b')
The idea is to draw 2 lines.
Please note that "I will have something like: c=[a b]" is not clear and therefore KSSV cannot suggest perfectly matching code.
@KSSV: +1
KSSV
KSSV le 16 Fév 2017
Modifié(e) : KSSV le 16 Fév 2017
a = rand(10,1) ;
b = rand(20,1) ;
posa = 1:length(a) ;
posb = length(a)+1:length(b)+length(a) ;
plot(posa,a,'r',posb,b,'b')
Auryn_
Auryn_ le 16 Fév 2017
Sorry if it was confusing. I try to explain it better: I do not want to draw 2 lines, but one. I want one line but with 2 different colours (half of the data in one colour, and the other half in a different colour).
Adam
Adam le 16 Fév 2017
A single line object cannot have multiple colours, but does it matter how many line objects you have?
Auryn_
Auryn_ le 16 Fév 2017
I have done it at the end with multiple plots. Thanks anyway!

Connectez-vous pour commenter.

Question posée :

le 16 Fév 2017

Commenté :

le 16 Fév 2017

Community Treasure Hunt

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

Start Hunting!

Translated by