Plot matrix by different colours

3 Ansichten (letzte 30 Tage)
cadebe
cadebe am 31 Jul. 2011
I'm new to MATLAB and would like to plot the elements of an array of 16 values onto a graph so that each is plotted in a different colour and markertype and that the colour and markertype is displayed with the name in the legend. The values in the array are stored in a 1 row, 16 column matrix. Each value is associated with a city name. I created a function file that loads the city names from a .csv file. I am using the plot (A) command. It seems to me MATLAB plots all the values at once and does not accept multiple colour and marker type assignment. How do I get around this? Do I use a for-loop to loop through the names and values in the matrix, using the hold all command? I'm not quite sure how to do this.

Antworten (1)

Jan
Jan am 31 Jul. 2011
A FOR loop and "hold all" sounds fine. But you cannot define a MarkerStyleOrder in opposite to LineStyleOrder and ColorOrder. This must be defined manually:
data = rand(1, 16);
figure;
hold('all');
MarkerOrder = '+o*x';
MarkerIndex = 1;
ColorOrder = lines(6); % see "doc colormap"
ColorIndex = 1;
H = zeros(1, 16); % Store handles
for i = 1:16
H(i) = plot(i, data(i), ...
'Marker', MarkerOrder(MarkerIndex), ...
'Color', ColorOrder(ColorIndex, :));
ColorIndex = ColorIndex + 1;
if ColorIndex > size(ColorIndex, 1)
ColorIndex = 1;
MarkerIndex = MarkerIndex + 1;
if MarkerIndex > size(MarkerIndex, 2)
MarkerIndex = 1;
end
end
end
legend(H, {'Name1', 'Name2', 'Name3', <etc>});

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by