Combining multiple plot statements.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 6 Okt. 2019
"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.
Akzeptierte Antwort
dpb
am 6 Okt. 2019
Bearbeitet: dpb
am 7 Okt. 2019
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
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!