Combining multiple plot statements.

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

采纳的回答

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