MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Kate on 11 Jun 2013

Hi there! I've attached a figure below which graphs changes in flux by season. Problem: Matlab is currently drawing lines in between the beginning and end of my seasons, making the graphs look odd. Is there a simple way to keep it from doing this?

Thanks a bunch!

subplot(2,2,3) plot(Fall(:,2),Fall(:,3)) title('Fall') subplot(2,2,4) plot(Wtr(:,2),Wtr(:,3)) title('Wtr') subplot(2,2,1) plot(Spr(:,2),Spr(:,3)) title('Spr') subplot(2,2,2) plot(Sum(:,2),Sum(:,3)) title('Sum')

Answer by the cyclist on 11 Jun 2013

Edited by the cyclist on 11 Jun 2013

Accepted answer

The default line style for plot() is the draw a line between each pair of points. If it is OK to not have *any* of those lines, then you could plot just the data points, for example

plot(Fall(:,2),Fall(:,3),'.')

However, if you want the lines *except* when it "backtracks", then probably need to split your data into separate series for plotting. You could do this by deftly inserting NaN into your vectors where the backtracking occurs. Here is a simple example of what I mean. Notice the difference between the two plots.

x1 = [1 2 3 1 2 3]; y1 = [1 2 3 3 4 5];

figure plot(x1,y1,'-')

x2 = [1 2 3 nan 1 2 3]; y2 = [1 2 3 nan 3 4 5];

figure plot(x2,y2,'-')

Angus on 12 Jun 2013

Ahhh, repeated 'x' values, that is how that happens hey? Good to know, I hadn't thought of that. As you mentioned above about it being multiple data years you might want to consider plotting it so that they are separate lines with distinct colors. I imagine this would help keep track of which year you were looking at.

xF = Fall(1:100,2) % Whatever the full range is xW = Wtr(1:100,2) %etc.

subplot(2,2,3) plot(xF,Fall(1:100,3),xF,Fall(101:200,3),xF,Fall(201:300,3)) %This will eliminate backtracks and give them distinct colors for each year title('Fall')

subplot(2,2,4) plot(xF,Wtr(1:100,3),xF,Wtr(101:200,3),xF,Wtr(201:300,3)) %The colors will match the ones used in the Fall plot title('Wtr')

%etc.

Glad you got it figured out, have a good one.

## 3 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78731#comment_154383

*Note, image uplaod isn't working. It's the first image here: http://oneshisttwoshistgreenshistblueshist.blogspot.com

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78731#comment_154450

It seems like it is plotting more than just a single data set for each plot, or the 'y' data domain is larger than the plotting window. Im not sure what is going on but what are the dimensions of Fall(:,2) and Fall(:,3)? What types are they? Ill see if I can find an example of this elsewhere.

Cheers, Angus

(You may want to avoid using variable names that are also built-in functions, such as sum, it may end up in confusing errors if you drop a capital)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/78731#comment_154601

Thanks Angus, helpful hints. Each season in this case includes multiple years of flux data. This is basically a check to make sure I don't have any wacky data years or to point out specific years of interest that are anomalous.

I appreciate the help!