How to change colors in my legend that is classified into three intervals?

3 Ansichten (letzte 30 Tage)
Hi!
I have a code that looks like this:
function class_post(lon, lat, var, class)
m = length(var);
hold on
color = [rgb('blue'); rgb('green'); rgb('red')];
% color = [rgb('red'); rgb('orange'); rgb('yellow'); ...
% rgb('green'); rgb('cyan'); rgb('blue'); rgb('purple')];
size = [2 4 6 8 9.2 10.5 12];
bins = {};
if isempty(class)
a = min(var);
b = max(var);
class = [a + (b - a) / 3: (b - a) / 3: b - (b - a) / 3];
bins = {sprintf(' < %1.3f', class(1))};
ind = find(var < class(1));
m_plot(lon(ind), lat(ind), 'o', 'color', color(1,:), 'markerfacecolor', color(1,:));
for i = 1:length(class)-1 % Loop until the second-to-last element
c = color(i+1,:);
s = size(i+1);
bins = [bins; sprintf('%1.3f - %1.3f', class(i), class(i+1))];
ind = find(var >= class(i) & var < class(i+1));
m_plot(lon(ind), lat(ind), 'o', 'color', c, 'markerfacecolor', c);
end
ind = find(var >= class(end)); % Use end to access the last element
m_plot(lon(ind), lat(ind), 'o', 'color', color(end,:), 'markerfacecolor', color(end,:));
bins = [bins; sprintf(' > %1.3f', class(end))];
else
for i = 1:length(class)
c = color(i,:);
s = size(i);
bins = [bins; sprintf('%1.3f - %1.3f', class(i), class(i+1))];
ind = find(var >= class(i) & var < class(i+1));
m_plot(lon(ind), lat(ind), 'o', 'color', 'k', 'MarkerSize', s, 'LineWidth', 1);
end
end
% Plot map details
m_gshhs_f('patch', [.8 .8 .8], 'LineWidth', 1.2);
m_grid('tickdir', 'out', 'linewidth', 1.2, 'xtick', 4, 'ytick', 5);
xlabel('Longitude (\circN)', 'fontsize', 12);
ylabel('Latitude (\circE)', 'fontsize', 12);
% For legend
h = legend(bins, 'location', 'EastOutside');
ax1 = gca;
pos = get(ax1, 'position');
I am trying to plot my data with three classes (<less than(blue), =no change(green) and >greater than(red)). However, when I run this code only the green and red colors shows. How can I fix this code to include all colors in my legend?
Thank you!
  5 Kommentare
Voss
Voss am 1 Mär. 2024
Bearbeitet: Voss am 1 Mär. 2024
@Sofia Quillope: General advice: avoid naming variables the same as built-in functions, e.g., naming your variable size will prevent you from using the built-in function size().
Dyuman Joshi
Dyuman Joshi am 1 Mär. 2024
class() is also a built-in function, which OP has used as a variable name.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 1 Mär. 2024
Try storing the line handles returned from the m_plot calls, and then use those line handles in the legend call. That fixed the problem for me when I tested it.
Something like this (note the new variable hl is the line handle array):
function class_post(lon, lat, var, class)
m = length(var);
hold on
color = [rgb('blue'); rgb('green'); rgb('red')];
% color = [rgb('red'); rgb('orange'); rgb('yellow'); ...
% rgb('green'); rgb('cyan'); rgb('blue'); rgb('purple')];
size = [2 4 6 8 9.2 10.5 12];
bins = {};
if isempty(class)
a = min(var);
b = max(var);
class = [a + (b - a) / 3: (b - a) / 3: b - (b - a) / 3];
bins = {sprintf(' < %1.3f', class(1))};
ind = find(var < class(1));
% pre-allocate line handle array:
hl = gobjects(1,numel(class)+1);
% store the first line in hl(1):
hl(1) = m_plot(lon(ind), lat(ind), 'o', 'color', color(1,:), 'markerfacecolor', color(1,:));
for i = 1:length(class)-1 % Loop until the second-to-last element
c = color(i+1,:);
s = size(i+1);
bins = [bins; sprintf('%1.3f - %1.3f', class(i), class(i+1))];
ind = find(var >= class(i) & var < class(i+1));
% store each line:
hl(i+1) = m_plot(lon(ind), lat(ind), 'o', 'color', c, 'markerfacecolor', c);
end
ind = find(var >= class(end)); % Use end to access the last element
% store the last line:
hl(end) = m_plot(lon(ind), lat(ind), 'o', 'color', color(end,:), 'markerfacecolor', color(end,:));
bins = [bins; sprintf(' > %1.3f', class(end))];
else
for i = 1:length(class)
c = color(i,:);
s = size(i);
bins = [bins; sprintf('%1.3f - %1.3f', class(i), class(i+1))];
ind = find(var >= class(i) & var < class(i+1));
m_plot(lon(ind), lat(ind), 'o', 'color', 'k', 'MarkerSize', s, 'LineWidth', 1);
end
end
% Plot map details
m_gshhs_f('patch', [.8 .8 .8], 'LineWidth', 1.2);
m_grid('tickdir', 'out', 'linewidth', 1.2, 'xtick', 4, 'ytick', 5);
xlabel('Longitude (\circN)', 'fontsize', 12);
ylabel('Latitude (\circE)', 'fontsize', 12);
% For legend
% explicitly tell legend which lines use in the legend:
h = legend(hl, bins, 'location', 'EastOutside');
ax1 = gca;
pos = get(ax1, 'position');
  1 Kommentar
Voss
Voss am 1 Mär. 2024
Unrelated to your question: note that in the else block
for i = 1:length(class)
c = color(i,:);
s = size(i);
bins = [bins; sprintf('%1.3f - %1.3f', class(i), class(i+1))];
ind = find(var >= class(i) & var < class(i+1));
m_plot(lon(ind), lat(ind), 'o', 'color', 'k', 'MarkerSize', s, 'LineWidth', 1);
end
you're going to have an error when i is length(class) because you try to access class(i+1).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by