I have two sets of data which can be plotted as a sets of discrete points given by (x,y) coordinates on a 2D plot. Is there some way that I can manually plot both sets of points, join the points to have two separate curves for the two data sets and then have a legend corresponding to the points used for the plotting? If easier, I could enter the data sets as arrays by creating the matrix and then plotting from it.
I did something similar before in this case it was something like a scatter diagram whereas here I need to join the points.

 Réponse acceptée

Cris LaPierre
Cris LaPierre le 24 Mar 2020
Something like this?
Use the hold command.
x = linspace(-pi,pi);
y1 = sin(x);
plot(x,y1)
hold on
y2 = cos(x);
plot(x,y2)
hold off

10 commentaires

If you want to manually specify the format, adjust the plot command accordingly
plot(x,y1,'r*')
Add a legend using the legend function. Use the default names, or specify your own.
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('cos(x)','cos(2x)')
Cris LaPierre
Cris LaPierre le 24 Mar 2020
You can learn about this and more in Chapter 9 of MATLAB Onramp. It's free, interactive, and self-paced.
Hi Cris, thanks for your response. I have something like this for blue dots and red dots
Model=plot(89,2.71, 'b.');
hold on
Data=plot(89,2.71, 'r.');
hold on
Data=plot(89,2.71, 'r.');
hold on
Data=plot(87,2.71, 'r.');
hold on
legend('Model','Data');
but for some reason the legend shows the blue dots for both 'Model' and 'Data', how can I correct this as I have not had this problem before.
The legend appears correctly for me. The bigger issue is you can't see the points. They are too small. I'd suggest using a different marker. I'd write your code like this
plot(89,2.71, 'b*');
hold on
plot(89,2.71, 'rs');
plot(89,2.71, 'rd');
plot(87,2.71, 'r^');
hold off
legend('Model','Data');
OK thanks, I'll change the pointer. The legend seems to show correctly in the case I have posted, but if you have multiple 'blue dots' as follows
Model=plot(1,6.27, 'b.');
hold on
Model=plot(2,6.23, 'b.');
hold on
Data=plot(89,2.71, 'r.');
hold on
Data=plot(89,2.71, 'r.');
hold on
Data=plot(87,2.71, 'r.');
hold on
legend('Model','Data');
then it just shows blue dots for both Model and Data.
Cris LaPierre
Cris LaPierre le 24 Mar 2020
Modifié(e) : Cris LaPierre le 24 Mar 2020
Yup. Your code isn't quite doing what you think it is. It might be helpful to open the plot browser to see what is going on (in the figure window menu, select View>Plot Browser.
You'll see that 5 separate series have been plotted on your figure. The legend command has assigned names to the first two. You think you are naming your series by assigning each plot to a variable. However, this is capturing a handle to the plot object. You don't need it. Legend names are assigned in the order the objects were plotted. So with your example, 'Model' is assigned to the first series (plot(1,6.27, 'b.')) and 'Data' is assigned to the second (plot(2,6.23, 'b.')).
Also, turn hold on once. After you are done plotting, turn hold off.
Take a few minutes and go through Chapter 9 of Onramp. You'll save yourself a lot of wasted time by learning how plotting works in MATLAB.
Hollis Williams
Hollis Williams le 3 Avr 2020
I have looked through Onramp but still cannot find a way to do the following: I have plotted the points as markers which creates the appearance of a curve on the plot, would it be possible to replace these dots with dashes to create a continuous dashed curve in the figure?
It sounds like you want to specify a line style rather than a marker. You can see the available line style, marker and color options here. As a simple example, you could do this.
plot(2,6.23, 'b--')
Hollis Williams
Hollis Williams le 3 Avr 2020
I did try this but it does not produce anything on the figure, I think the trouble is that I am plotting points one-by-one manually rather than creating a curve in the first place. I am just wondering if there is some way to plot all the points as dashes such that each dash points towards the next point and you end up with a dashed curve.
Cris LaPierre
Cris LaPierre le 3 Avr 2020
Bad example. Sorry. Yes, line styles don't apply if you are plotting the points one at a time. You'll need to use a marker. Check out the link I shared. That shows you the available marker styles.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by