MATLAB Answers

Dynamic color for a plot

25 views (last 30 days)
Avishek Dutta
Avishek Dutta on 31 May 2012
Hi all,
I am a beginner so forgive my stupid questions.
I am trying to classify some IMU data. I want different movements with different colors. Instead of complicating things with my details, I will put this simply.
a = [ 1 ; 2 ]
b = { 'b-' ; 'g-' }
When try to plot,
I get this error
??? Error using ==> plot Error in color/linetype argument
The real code looks like
axesAcc_X = plot(Acc_X(:,1),char(colorVector{:,1}));
Both vectors are of same size and I have ensured that the color type matches the desired data inside the data vector.
Can someone please help me?


Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 31 May 2012
You cannot specify multiple colors (or line styles, or markers) when you use a single Y for plot, even when Y has multiple columns. The syntax only allows for one linespec for each Y argument, and only if you interleave them
plot(x1, y1, 'b-', x2, y2, 'g-')
If you use a property name / value pair such as 'linestyle', '-', then only the last one of those in the plot() call will be paid attention to, and it will apply to all of the values plotted.
The solution is to record the handles output by plot (note that they are not "axes" handles, they will be lineseries handles), and to set() the linestyle etc. properties for each as appropriate.
Or set the axes ColorOrder property before plotting.
The MATLAB File Exchange contribution "plt" is more flexible I understand.


Avishek Dutta
Avishek Dutta on 31 May 2012
Thanks Walter,
But like I said I am a newbie, so it seems to me that I am not specifying multiple colors.
For each data point in Acc_X the corresponding color/style from my colorVector be read and assigned, thats what I want.
I have recording frequencies of 120Hz and 10Hz. So i don't know the size of each chunk. And the file is user input.
As a last resort I can try looping, but I dont want 28 loops, just to color my points.
I have no expereince with lineseries handles or ColorOrder. But thanks in advance if they solve my issue.
Walter Roberson
Walter Roberson on 31 May 2012
plot() cannot be used to change the color on a point-by-point basis. scatter() allows changing the color on a point-by-point basis, but scatter() does not draw connecting lines.

Sign in to comment.

Image Analyst
Image Analyst on 31 May 2012
You have a 1D set of numbers: Acc_X(:,1). Are you trying to plot it in both blue and green somehow???? Can you explain further?


Avishek Dutta
Avishek Dutta on 31 May 2012
Thank you for your interest,
I have readings in Acceleration(x,y,z), Gyro (x,y,z) etc {in total 28 coloums} , for every scenario like run, walk, sit, stair up/down etc {total 7 scenarios}, in 7 different files.
Task is to plot data that user choses like acc or gyr or mag but for all scenarios together.
Hence Acc_X is the 1D acceleration vector for all scenarios.
I want to plot each of the 7 chunks inside in different colors.
Hope this clears it up.
Walter Roberson
Walter Roberson on 31 May 2012
What does the second dimension of Acc_X represent? Based on your comments to me, it sounds like it is not the scenario number, as you indicate you have different lengths for for each scenario.
If Acc_X(:,1) is the X acceleration for all of the scenarios together, then is there any marker (such as NaN) to indicate when one scenario ends and the next begins? Or is there a vector indicating the starting point of each scenario?
Avishek Dutta
Avishek Dutta on 31 May 2012
Acc_X is 1D, one coloum full of records/points.
No there is no indicator, but Acc_X is mine, i make it from input data by concatenating all scenario file.
I can put a marker in, but please explain what to do next.

Sign in to comment.

Stephen on 1 Jun 2012
if you want to change a bunch of stuff with plot, you could do it like this:
for t=1:5
set(h(t),'Marker',mark(t),'color',[randi(5)/5 1-t/5 t/5])
or something similarly convoluted and overcomplicated


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!