0

In Matlab:

How do I modify plot(x,y,'o'), where x=1:10 and y=ones(1,10), such that each point in the plot will have a random shape?

And how can I give it colors chosen from a scheme where the value at x=1 is the darkest blue, and x=10 is red (namely some sort of heat map)?

Can this be done without using loops? Perhaps I should replace "plot" with a different function for this purpose (like "scatter"? I don't know...)? The reason is that I am plotting this inside another loop, which is already very long, so I am interested in keeping the running-time short.

Thanks!

1
  • 1
    Assuming the runtime will be too long because of the nested loops is not necesarily correct. I'd test it before claiming so. Commented Apr 5, 2019 at 12:10

1 Answer 1

5

First, the plain code:

x = 1:20;
nx = numel(x);
y = ones(1, nx);

% Color map
cm = [linspace(0, 1, nx).' zeros(nx, 1) linspace(1, 0, nx).'];

% Possible markers
m = 'o+*.xsd^vph<>';
nm = numel(m);

figure(1);
hold on;

for k = 1:nx

  plot(x(k), y(k), ...
    'MarkerSize', 12, ...
    'Marker', m(ceil(nm * (rand()))), ...
    'MarkerFaceColor', cm(k, :), ...
    'MarkerEdgeColor', cm(k, :) ...
    );

end

hold off;

And, the output:

Output

Most of this can be found in the MATLAB help for the plot command, at the Specify Line Width, Marker Size, and Marker Color section. Colormaps are simply n x 3 matrices with RGB values ranging from 0 to 1. So, I interpreted the darkest blue as [0 0 1], whereas plain red is [1 0 0]. Now, you just need a linear "interpolation" between those two for n values. Shuffling the marker type is done by simple rand. (One could generate some rand vector with size n beforehand, of course.) I'm not totally sure, if one can put all of these in one single plot command, but I'm highly sceptical. Thus, using a loop was the easiest way right now.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the good response, however I'm afraid that the loop solution is not good enough, since I'm interested later-on in plotting in a loop, after I manipulate the values in the array.. If you can help me find a solution without a loop, would be much better :)
@user1611107 What you want seems very difficult to achieve without a loop. Why don't you want to use a loop? You can nest it under your other loop
@user1611107 you should edit your question with more information so that the limitation and problems become clear
I am simply running some code that changes the values in each of the positions in the array, and I am looking for a visual representation that will allow me to track the dynamics and see with my eyes when one value became larger the it's predecessor or few of it's predecessors. I have thousands of array entries, and I am plotting at each step in a loop which runs thousands of times, so I don't want the plotting procedure to delay the rest of the code too much. It's just about keeping the running time short.. But if it's not possible without loops, then I'll accept the answer above
@user1611107: search for animating plots in MATLAB. It is much more efficient to update the XData and YData properties of the line objects than to plot a new line every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.