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 JanisJuly 24, 2026No 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

    MATLAB Function Reference

    July 24, 2026

    Automatically Generated Legend with Descriptive Labels for Data Series in MATLAB

    July 24, 2026

    Add Legend to Graph – Matlab Legend

    July 24, 2026
    Leave A Reply Cancel Reply

    Search

    SLOT GACOR

    Recent Posts

    What Is YOKTOGEL? An In-Depth Overview of the Platform, Core Features, Accessibility, User Experience, and Important Information for New Users

    August 18, 2026

    The Complete Walkthrough Detailing How to Calculate Spread in Forex Quickly

    August 7, 2026

    Expert-Level Data Science Programs That Top Companies Actually Want on Your Resume

    August 4, 2026

    MATLAB Function Reference

    July 24, 2026

    Automatically Generated Legend with Descriptive Labels for Data Series in MATLAB

    July 24, 2026

    Add Legend to Graph – Matlab Legend

    July 24, 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

    What Is YOKTOGEL? An In-Depth Overview of the Platform, Core Features, Accessibility, User Experience, and Important Information for New Users

    August 18, 2026

    The Complete Walkthrough Detailing How to Calculate Spread in Forex Quickly

    August 7, 2026
    Contact Us

    Phone: Whatsapp

    Mail: tech4links@gmail.com

    เว็บหวยออนไลน์ | UFABET | kết quả bóng đá | สล็อต | สล็อต | สล็อตเว็บตรง | แทงบอลโลก | hitclub | ufabet | ufabet เข้าสู่ระบบ | jalalive gratis | https://nhacaiuytin100.com/ | M88 | W88 | Fun88 | สล็อต | sunwin | sunwin | go88 | bgibola | sunwin | หวยออนไลน์ | เว็บยูฟ่า | เบทฟิก | ufabet | ufabet เข้าสู่ระบบ | สล็อตเว็บตรง | สล็อตเว็บตรง

    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