Plotting Two Data Sets on a Figure
显示 更早的评论
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.
采纳的回答
Something like this?

x = linspace(-pi,pi);
y1 = sin(x);
plot(x,y1)
hold on
y2 = cos(x);
plot(x,y2)
hold off
10 个评论
If you want to manually specify the format, adjust the plot command accordingly
plot(x,y1,'r*')
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)
hold on
y2 = cos(2*x);
plot(x,y2)
legend('cos(x)','cos(2x)')

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
2020-3-24
编辑:Cris LaPierre
2020-3-24
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.
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--')
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.
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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Legend 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
