How can I create a legend for an event data plot?

I have created plots for event time-series data. The input data consists of csv files with 3 colums: onsett time, ofsett time, code (integers, from 1-9). I have first created a color array, to assign a color to each code, which looks something like this:
momAffectcolor={1, [RGB triplet];
2, (RGB triplet]; etc}
Following is a for loop that loops through each code consecutively, graps the right color from the color array, and plots the code at the right time (from onsett to ofsett). Now, I would like to create a legend for this plot based on the color array, but have not succeeded to do so. Any ideas are welcome!

 Risposta accettata

Adam Danz
Adam Danz il 10 Giu 2020
The easiest method IMO is to use the DisplayName property of (most) graphics objects.
It defines all of the legend strings before the loop and then applies the legend strings within the loop.
Alternatively, you could build the legend strings within the loop using sprintf() or compose().

4 Commenti

Thank you for your quick reply! Sadly it does not solve my legend problem, I think this is because of the specific way in which event data is plotted. My code loops through each eventcode (e.g. mother giving an instruction, mom being silent etc) consecutively, and plots each event occurence for this specific eventcode, before turning ot the next eventcode category. I have added a figure display of what this looks like, with the legend that I obtain when I use the DisplayName option.
This is the complete code, which might be helpfull for considering adaptive options for creating a legend:
figure(pID);
hold on;
for curKAffectState=[1:9]
curKAffColor=KAffectColor{curKAffectState,2};
curKAffValue=KAffectColor{curKAffectState,1};
curKAxisPos=KAffectColor{curKAffectState,3};
indices=find(kData(:,3)==curKAffValue);
dataKRows=kData(indices,:);
countKRows=size(dataKRows,1);
for row=1:countKRows
hold on
plot(dataKRows (row, [1 2]),[curKAxisPos curKAxisPos], 'Color',curKAffColor, 'LineWidth',10);
end
end
I am familiar with that problem. In fact, I wrote a function that removes the duplicate legend strings.
First, you need to assign the legend names using DisplayName as I mentioned in my answer. For each color, you should have a cell array of legend strings that define the color.
For example, (adapt to your code)
curKAffectState{1,4} = 'Condition 1';
curKAffectState{2,4} = 'Condition 2'; % etc....
When you call plot() you are assigning a color on each loop so you'll also assign the same DisplayName on each loop.
For example, (adapt to your code)
curKAxisLegendName=KAffectColor{curKAffectState,4};
plot(_____, 'DisplayName', curKAxisLegendName);
Then, after the for-loop, you can create the legend using
uniqueHandles = legendUnq(gca);
legend(uniqueHandles)
where gca is your axis handle. The legendUnq() function can be found here: https://www.mathworks.com/matlabcentral/fileexchange/67646-legendunq
Thank you very much! I know it is not 'cool' to use this word, but it is very cool to see that it works!
"Cool" is still a cool word ;)
"Rad", on the other hand, might be outdated :D
If you have any problems with the the legendUnq function, please let me know. If it works well, you can support it by leaving a rating on the file exchange.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by