How to produce a graph with 2 sets of data on matlab?

Hi everyone, so i've created a code that solves an iteration for me that gives me the correct Temperature for a composition.
This leads to me needing to code for a graph Bubble Point Temperature v Composition and Dew Point Temperature v Composition.
Both in 1 graph so that I have 2 curves in the same graph.
How would I go about doing this?
Also is there a way for the graph to be produced automatically right after the code i have already coded of for provides me with the temperature?
If this doesn't make sense i'm more than happy to re-explain, i'm currently pretty sleep deprived haha.
Any help is appreciated!

Réponses (2)

There are several ways. The easiest way is to use the hold function.
Example:
figure(1)
plot(x1, y1)
hold on
plot(x2, y2)
hold off
grid

7 commentaires

Alai Yosief
Alai Yosief le 9 Nov 2015
Modifié(e) : Alai Yosief le 9 Nov 2015
Thanks matey, i forgot to elaborate, for both set of data i'll have:
Composition of 0, 0.1, 0.2... etc. 1 and each will have a different temperature.
In that case what would my x1 and y1 be? since they'll be curves and not linear
My pleasure.
In my Answer, ‘x1’, ‘y1’ and the others represent vectors of your independent and dependent variables respectively. In each plot call, both vectors have to have the same number of elements.
Alai Yosief
Alai Yosief le 10 Nov 2015
Modifié(e) : Alai Yosief le 10 Nov 2015
Hey, I've been reading up on graphs and have a rough idea however I'm still getting caught up on the vector part. So here is the data I'm trying to plot: x - 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1 against, Temperature - 35, 34, 34, 34, 33, 32.9, 32.8. 32.5, 32.7, 31, 30
Try this:
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
Temperature = [35, 34, 34, 34, 33, 32.9, 32.8, 32.5, 32.7, 31, 30];
figure(1)
plot(x, Temperature)
grid
xlabel('x')
ylabel('Temperature °C')
Fantastic! that worked perfectly. So I;m trying to plot 2 sets of data and I've written the following so far however it is only plotting for 1 curve instead of the both: x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
Temperature = [272.78, 262.92, 259.74, 258.66, 258.30, 258.19, 258.18, 258.27, 258.68, 260.31, 266.70];
Temperature1 = [272.8, 270.4, 267.9, 265.2, 262.3, 259.3, 258.3, 258.5, 260.6, 262.7, 264.8 ,266.7];
figure(1)
plot(x, Temperature)
hold on
plot(x,Temperature1)
hold off
grid
xlabel('x')
ylabel('Temperature K')
Any Suggestions?
Thanks!!!!
You have 12 elements in Temperature1, but only 11 in x and Temperature. Get rid of one element in Temperature1 and you are done.
Star Strider
Star Strider le 10 Nov 2015
Modifié(e) : Star Strider le 10 Nov 2015
You have to have the same number of elements in x and Temperature in the first plot, and x and Temperature1 in the second.
One way to do that is to create x1 and x2:
x1 = linspace(0, 1, length(Temperature));
x2 = linspace(0, 1, length(Temperature1));
figure(1)
plot(x1, Temperature)
hold on
plot(x2, Temperature1)
hold off
grid
legend('Temperature', 'Temperature1')
xlabel('x')
ylabel('Temperature (°C)')

Connectez-vous pour commenter.

Thorsten
Thorsten le 10 Nov 2015
Modifié(e) : Thorsten le 10 Nov 2015
I deleted the final element of Temperature1, such that x, Temperature, and Temperature1 have the same number of elements:
x = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
Temperature = [272.78, 262.92, 259.74, 258.66, 258.30, 258.19, 258.18, 258.27, 258.68, 260.31, 266.70];
Temperature1 = [272.8, 270.4, 267.9, 265.2, 262.3, 259.3, 258.3, 258.5, 260.6, 262.7, 264.8];
To plot, use:
plot(x, Temperature)
hold on
plot(x, Temperature1)
or
plot(x, Temperature, x, Temperature1)
or
plot(x, [Temperature; Temperature1]')

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by