Question

I've to modify a figure, but I wouldn't like to recreate it since I have the .fig.

I would like to simply change the color of the legend maintaining the order.

for example I have

blue marker -> cluster 1

green marker -> cluster 2

red marker -> cluster 3

light blue marker -> cluster 4

magenta marker -> cluster 3

and I want to invert green and light blue

blue marker -> cluster 1

light blue marker -> cluster 2

red marker -> cluster 3

green marker -> cluster 4

magenta marker -> cluster 5

Is there any way to do this without create again the figure?

ps it's a scatterplot

enter image description here

you can download the figure here: https://drive.google.com/file/d/0B3vXKJ_zYaCJMS1feHFSaHp4R28/edit?usp=sharing

Was it helpful?

Solution

If you change the Color setting of the plots, it should change the legend automatically:

h = get(gca,'Children');
c = get(h,'Color');

c should be a cell array of colors. Presuming the handles are in the same order as the legend this should work:

set(h(2),'Color',c{4})
set(h(4),'Color',C{2})

Handle order can change depend how you created the figure, though, so you might want to just double-check which two you're swapping first.


What value you need to change depends on the exact type of plot. Usually it's just a matter of digging down into the Children for the axes to find the right bits to change.

In your case:

a = get(gca, 'Children');

q = get(a,'CData');
% there are six handles here
% I just looked at the CData and decided which to swap
set(a(3),'CData',q{5})
set(a(5),'CData',q{3})

Here, the first handle says what point is which so you'll need to swap those colors as well (thanks to Hoki for pointing out my error). Not the most elegant but a quick fix with ismember:

col = q{1};
col2 = col;
n3 = ismember(col, q{3},'rows');
n5 = ismember(col, q{5},'rows');
col2(find(n3),:)=repmat(q{5},[length(find(n3)) 1]);
col2(find(n5),:)=repmat(q{3},[length(find(n5)) 1]);
set(a(1),'CData',col2);

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top