Plotting Two Data Sets on a Figure
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
Cris LaPierre
2020년 3월 24일
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)')

Cris LaPierre
2020년 3월 24일
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.
Cris LaPierre
2020년 4월 3일
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)
