Combining multiple plot statements.

조회 수: 2 (최근 30일)
Mughees Asif
Mughees Asif 2019년 10월 6일
편집: dpb 2019년 10월 7일
I want to combine multiple plot statements into one, for example, how could I write the following code using one plot statement?
plot(1,-1,'k*')
plot(1,(1/3),'k*')
plot(1,1,'k*')
plot(1,-5,'rv', 'MarkerFaceColor', 'r')
plot((1+sqrt(5)),0,'g^', 'MarkerFaceColor', 'g')
plot((1-sqrt(5)),0,'g^', 'MarkerFaceColor', 'g')
Thank you.
  댓글 수: 4
Stephen23
Stephen23 2019년 10월 6일
"I just wanted to reduce reusing the same statements to make it a bit cleaner?"
Merging them will be much more confusing. Your current code is clear, legible, and easy to debug, I don't see any reason to change it.
Mughees Asif
Mughees Asif 2019년 10월 6일
Ok. Thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2019년 10월 6일
편집: dpb 2019년 10월 7일
As is, the above code snippet leaves only the result of
plot((1-sqrt(5)),0,'g^', 'MarkerFaceColor', 'g')
on the figure because there is no hold on in the sequence; hence each subsequent call to plot replaces the previous data. That is probably not what is intended.
plot(1,-1,'k*')
hold on
plot(1,(1/3),'k*')
plot(1,1,'k*')
plot(1,-5,'rv', 'MarkerFaceColor', 'r')
plot((1+sqrt(5)),0,'g^', 'MarkerFaceColor', 'g')
plot((1-sqrt(5)),0,'g^', 'MarkerFaceColor', 'g')
will produce a different plot entirely.
While I can't disagree w/ the other two respondent's individual choice; I'd probably recast myself somewhat at least into something more like
% initial point values to plot--remove data to be able to change easily if need to and w/o
% having to edit the code itself...
x1=[1;1;1]; y1=[-1;1/3;1];
x2=1; y2=-5;
x3=[5;5]; y3=[0;0];
hS1=scatter(x1,y1,'k*');
hold on
hS2=scatter(x2,y2,'rv','filled');
hS3=scatter(1+[-1;1].*sqrt(x3),y3,'r^','filled');
ylim([-5.5 1.5])
box on
legend('One','Two','Three','Location','southeast')
produces
untitled.jpg
Many alternative ways depending upon just what x,y represent in the code to make variable names reflect what they are would be the most important thing for readability.
The logic in the above is to create each set with the same marker style as a single scatter object on the assumption one would have three separate things to name by the linestyles. If that isn't so, then making the line handles match up to the legend entries would be the better choice.
With the individual symbols, plot creates a separate line handle for each point so there are six line handles instead of three.
Again, there's no absolute here; user preference and comments will help however is the end choice.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by