Close Menu
MatlabLegend
    Facebook X (Twitter) Instagram
    MatlabLegend
    • Home
    • Matlab
    • Fashion
    • Gadgets
    • Biography
    • Tech News
    • Tips & Tricks
    MatlabLegend
    Home»Matlab»How to add legend to the Figure?
    Matlab

    How to add legend to the Figure?

    JanisBy JanisJanuary 12, 2025No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    How to add legend to the Figure?
    How to add legend to the Figure?

    “I have a plot where I’m attempting to add legends, including shaded rectangles. Please note that the rectangles are objects, but they aren’t appearing in the legend.”

    “I’ve also included a part of my code for reference. How can I add a legend for the rectangle, using a small rectangle of the same color as the symbol?”

    % plot data
    h = figure;
    plot(xWithTone,yWithTone,'Color',[0.9 0.7 0.1],'LineWidth',1);
    hold on;
    plot(xNormalized,yNormalized,'b','LineWidth',1);
    plot(xNormalized(1),yNormalized(1),'go','MarkerSize',10,'LineWidth',2);
    plot(xNormalized(end),yNormalized(end),'ro','MarkerSize',10,'LineWidth',2);
    % set figure limit
    maze = {'maze2','maze1','maze3','maze4'};
    figureLimit = {{[-0.2 1.2],[-0.2 1.2]},{[-1.2 0.2],[-0.2 1.2]}, ...
        {[-1.2 0.2],[-1.2 0.2]},{[-0.2 1.2],[-1.2 0.2]}};
    % get the index in maze array
    mazeIndex = find(ismember(maze,subject_data.mazenumber));
    xlim(figureLimit{mazeIndex}{1}); ylim(figureLimit{mazeIndex}{2});
    % create rectangles
    switch mazeIndex
        case 1
            % Maze2 a.k.a 1st quadrant
            % rectangles are denoted according to feeder numbers in this maze
            r1 = rectangle('Position',[0.8 0 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r2 = rectangle('Position',[0 0 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r3 = rectangle('Position',[0 0.8 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r4 = rectangle('Position',[0.8 0.8 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r = [r1,r2,r3,r4];
            set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
            rectangle('Position',[0.25 0.25 0.5 0.5],'EdgeColor','none', ...
                'FaceColor',[1 0 0 0.2]);
        case 2
            % Maze1 a.k.a 2nd quadrant
            % rectangles are denoted according to feeder numbers in this maze
            r1 = rectangle('Position',[-1 0.8 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r2 = rectangle('Position',[-1 0 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r3 = rectangle('Position',[-0.2 0 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r4 = rectangle('Position',[-0.2 0.8 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r = [r1,r2,r3,r4];
            set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
            rectangle('Position',[-0.75 0.25 0.5 0.5],'EdgeColor','none', ...
                'FaceColor',[1 0 0 0.2]);
        case 3
            % Maze3 a.k.a 3rd quadrant
            % rectangles are denoted according to feeder numbers in this maze
            r1 = rectangle('Position',[-0.2 -1 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r2 = rectangle('Position',[-0.2 -0.2 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r3 = rectangle('Position',[-1 -0.2 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r4 = rectangle('Position',[-1 -1 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r = [r1,r2,r3,r4];
            set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
            rectangle('Position',[-0.75 -0.75 0.5 0.5],'EdgeColor','none', ...
                'FaceColor',[1 0 0 0.2]);
            legend({'feeders','offer','central zone'},'Location','northeastoutside');
        case 4
            % Maze4 a.k.a 4th quadrant
            % rectangles are denoted according to feeder numbers in this maze
            r1 = rectangle('Position',[0 -0.2 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r2 = rectangle('Position',[0.8 -0.2 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r3 = rectangle('Position',[0.8 -1 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r4 = rectangle('Position',[0 -1 0.2 0.2],'EdgeColor','none', ...
                'FaceColor',[0.3 0.3 0.3 0.3]);
            r = [r1,r2,r3,r4];
            set(r(subject_data.feeder),'FaceColor', [1 1 0 0.3]);
            rectangle('Position',[0.25 -0.75 0.5 0.5],'EdgeColor','none', ...
                'FaceColor',[1 0 0 0.2]);
        otherwise
            warning('Unexpected maze number.')
    end
    % add legend
    legend({'before trial tone','after trial tone','location at tone', ...
        'final location','feeders','offer','central zone'}, ...
        'Location','northeastoutside');
    Janis
    • Website

    Janis is the creator of Matlab Legend, an engineer and tech enthusiast passionate about simplifying MATLAB, AI, and tech concepts. Through practical guides and insights, they aim to empower learners and professionals worldwide.

    Related Posts

    Legend Behavior in Data Visualization: Automatic Labeling and Updates

    February 5, 2025

    Greek Letters and Special Characters in Chart Text

    January 31, 2025

    Make the Graph Title Smaller

    January 31, 2025
    Leave A Reply Cancel Reply

    Search
    Recent Posts

    How to Book a Bus from Kelantan to Melaka Online

    April 2, 2026

    Kineret Karen Ben Yishay Moore: Exploring the Meaning, Heritage, and Identity Behind the Name

    February 28, 2026

    Kevin Corke Biography: Wife, Spouse, and Family Insights

    February 19, 2026

    Elena Moussa Height: Discover How Tall Greg Gutfeld’s Wife Really Is and Why It Matters

    February 18, 2026

    Kurt Perez and The Blacklist: The Real Story Explained

    February 17, 2026

    Jaguar Wright’s Net Worth in 2026: Inside the Neo-Soul Singer’s $2.5 Million Success Story

    February 17, 2026
    About Us

    MatlabLegend is your go-to hub for mastering MATLAB with clarity and confidence. Explore expert insights, step-by-step tutorials, and practical guides designed for beginners and professionals alike.

    Whether you're starting out or advancing research and engineering projects, MatlabLegend helps you learn faster and apply MATLAB skills effectively every day. #MatlabLegend

    Popular Posts

    How to Book a Bus from Kelantan to Melaka Online

    April 2, 2026

    Kineret Karen Ben Yishay Moore: Exploring the Meaning, Heritage, and Identity Behind the Name

    February 28, 2026
    Contact Us

    Phone: Whatsapp

    Mail: tech4links@gmail.com

    เว็บหวยออนไลน์ | UFABET | kết quả bóng đá

    Copyright © 2026 | All Rights Reserved | MatlabLegend
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    • Write for Us
    • Sitemap

    Type above and press Enter to search. Press Esc to cancel.

    WhatsApp us