MATLAB Answers

How to plot a line with repeated values of x?

51 views (last 30 days)
Erin Gibbons
Erin Gibbons on 20 Feb 2020
Commented: Fede suad on 5 Aug 2020
I have collected spectral data from two instruments and stitched the spectra together (simple concatenation). I want to make a plot tht shows how the spectra have been stitched together.
Spectrum 1 ranges from 180-1050, and Spectrum2 ranges from 120-1200. When I plot them, the lines overlap because the x values are repeated in each range.
How can I make Spectrum2 plot after Spectrum1 on it's own x-values?
This is what I have written:
data = xlsread('_______','Load_LL','D1:SJE4');
x = data(1,:);
PC1 = data(2,:);
PC2 = data(3,:);
PC3 = data(4,:);
plot(x,PC1,'-k',...
'LineWidth',0.75);
ylim([-0.3 0.1])
ylabel('PC-1');
set(h1,'FontName','Times New Roman','FontSize',10);
set(h1,'xtick',[])
And I get this:
I want something that looks more like this:
I tried plotting each spectrum as a subplot, which got me closer to my goal, but the two spectra should not take up the same space on the x axis (Spectrum1 has farrrr more variables).
How can I plot each y value with it's associated x vlue without getting overlap between repeted x values?

  2 Comments

dpb
dpb on 20 Feb 2020
"Spectrum 1 ranges from 180-1050, and Spectrum2 ranges from 120-1200"
What do these value represent; simply point numbers datapoints in the spectra or actual values of whatever the real x-axis should be.
"The two spectra should not take up the same space on the x axis (Spectrum1 has farrrr more variables)."
What does "variables" refer to here--again the number of points or some not provided independent measure that's what you really should be plotting against?
The first plot doesn't make sense with the code; there is only one vector in a plot statement so that's not possible to have three overlapping lines unless all the data for the spectrum are in the one row of data(2,:) In which case, what are the other two?
Probably easier if you'll attach the raw data file so somebody can actually see what's in the file...but seems like you're missing the key piece of info of what the x-axis variable should be.
Fede suad
Fede suad on 5 Aug 2020
Hey there, could you solve it? The answer that was here didn't help and I'm having the same problem.

Sign in to comment.

Answers (1)

Athul Prakash
Athul Prakash on 23 Feb 2020
Looks like you want the plots to appear side-by-side.
Maybe you can concatenate 'PC1', 'PC2' and 'PC3' into one longer vector, while also creating a different x axis for plotting?
newY = [PC1, PC2, PC3];
distX = max(x)-min(x)+1;
newX = [x, x+distX, x+2*distX];
plot(newX, newY);
Hope this helps you.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by