Plotting 3 lines with different x values in one single plot?

Hi, I have data of absorbtion spectra of three different materials and I need to plot all of them on the same plot. The problem is that the X for each one isn't the same. For all of them the X axis is the wavelength but in one material this value jumps in 2 and in other it jumps in 25. This means that the wavelength (x value) of each material has a different length and not precisly the same values. However, I need all of them in one plot as in the following image:
My idea is to create a loop that eliminates the wavelengths that not match between the three materials (three vectros) but I don't know how to do it.
Thank you :)

回答(1 个)

Use the hold function to plot them individually.
Example —
x1 = sort(rand(1,10));
y1 = rand(size(x1));
x2 = sort(rand(1,27));
y2 = rand(size(x2));
figure
plot(x1, y1)
hold on
plot(x2, y2)
hold off
grid
.

12 个评论

This is what I get:
In addition, here is my code:
lamda.hb = Hemoglobin{:,1} %The first column of Hemoglobin is the x1 (the wavelength)
so2 = Hemoglobin{:,2}*0.1 %Part of y1
hb = Hemoglobin{:,3}*0.9 %Second part of Y1
epsilon = (so2 + hb).*lamda.hb %Part of Y1
x1 = lamda.hb;
y1 = 0.0054.*0.07.*epsilon; %Y1
lamda.water = water{:,1}; %The first column of water is the X2 (values from 200 to 90,000 with jumps of 25 at first, and than in jumps of 10)
mua.water = water{:,2}; %This is part of Y2
x2 = lamda.water;
y2 = 0.0054.*mua.water.*0.75;
figure
plot(x1, y1)
hold on
plot(x2, y2)
hold off
grid
I also get nothing because I do not have those matrices to experiment with.
If I did, I might be able to do something with them, or at least figure out what the problem is.
EDIT —
Use the save function to save the matrices to a .mat file and use the paperclip icon to upload it here.
.
The X axis is the wavelength and the Y axis is the absorbtion. (For Hemoglobin I need to merge both values)
Thank you
My pleasure.
The plot code works, however the linear axes do not.
Using the code you posted (that I will not re-post here) reveals that the variable dimensions are such that it is necessary to plot them on loglog axes:
figure
loglog(x1, y1)
hold on
plot(x2, y2)
hold off
grid
legend('\epsilon', 'H_2O')
producing:
That is the best I can do with your data to produce the plot. (On a linear scale, ‘epsilon’ plots as a vertical line on the left axis, and ‘water’ plots as a horizontal line in the lower axis. They are both present, although invisible for all practical purposes.)
.
ok perfect! I got the same graph :)
The last thing; I need to add another line named "Total", it includes Y values of the previous lines (water and hemoglobin). My question is which X do I use?
That may not be possible, since those two vectors are different lengths.
You might be able to use the interp1 function to interpolate them so that ‘Total’ to to the correct length for the appropriate ‘X’ value ( that may require interpolating ‘Total’ again), however I would have to know more in order to be certain.
Note that interpolation creates data that were not in the original data, and while this may be appropriate in some situations, it would not be in every situation. Be very careful with that.
The total line is in order to see the spectra for both materials together (water and hemoglobin). In theory the X axis is the same and refers to Wavelength. Is there a way to jus define a X axis with wavelength values? this values obviously will include the x values of hemoglobin and water..
You can see it on the graph I pasted here a few comments before. There are 3 lines, 1 of water, 1 of hemoglobin and the black one includes both values.
I cannot determine what you want to do.
The ‘Hemoglobin’ variable is a (376x3) table.
The ‘water’ variable is a (231x2) table.
It is not possible to add these tables together, or do any other element-wise opearations on them. They have different sizes.
It is not possible to add individual columns from them together. They have different lengths.
Exactly what vectors do you want to combine?
How do you want to combine them?
What vector do you want to plot them against?
Note that none of this may be possible without severely distorting the data. That would make any calculations you do with the distorted data unreliable (and unpublishable, if that is your intent).
I can adapt the values to the short variable and ignore the rest values of Hemoglobin which are out of the range.
Y1 is th Hemoglobin after merging two columns, so Hemoglobin is now (376x2). So I need to create a new tamble of Hemoglobin merged, I can ignore the values that don't match the water range.
So I need a new line consisting of Y1 and Y2.
Please be more specific.
Y1 is th Hemoglobin after merging two columns, so Hemoglobin is now (376x2).
What does ‘merge’ mean?
What is ‘Y2’?
What are the new variables?
How are they calculated?
What do you want to do with them?
Posting the relevant parts of your code that creates the variables would definitely help.
.
I am trying to use the interp function but I get an error alert.
What I need to do is to create the following graph:
The values and data are taken from the mat.files attached (water and Hemoglobin).
The X axis is the wavelength which is the first column in each file. The Y axis is the absorbstion spectra, which is a calculation I was requested to do (appears in the code attached below). In water it's a simple calculation, while in Hemoglobin I was asked to summ columns 2 and three, and I did so.
Finally I need to add a third line which is summing the first two. Example, in x wavelength I need to summ the Y value of Hemoglobin and water. In orther to do that I understan that I must have two vectors with the same length. This is when I tried using interp as following: Y3 = interp(x,1.627) but it didn't work. (1.627 is the ratio between Hemoglobin and water).
In addition, I've attached the instructions for this project, it might help.
lamda.hb = Hemoglobin{:,1};
so2 = Hemoglobin{:,2}*0.1;
hb = Hemoglobin{:,3}*0.9;
epsilon = (so2 + hb).*lamda.hb;
x1 = lamda.hb;
y1 = 0.0054.*0.07.*epsilon;
lamda.water = water{:,1};
mua.water = water{:,2};
x2 = lamda.water;
y2 = 0.0054.*mua.water.*0.75;
y3 = interp(x2,1.627);
figure
loglog(x1, y1, 'r')
hold on
plot(x2, y2, 'b')
hold off
grid
legend('7% Blood ', '75% water')
I still have no idea what you want to do.
Try this:
y3 = interp1(x1, y1, x2, 'linear','extrap');
figure
loglog(x1, y1)
hold on
plot(x2, y2)
plot(x2, y2+y3)
hold off
grid
legend('\epsilon', 'H_2O', 'y_2+y_3')
.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by