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

    Ironmartonline Reviews: Complete Guide to Buying Heavy Equipment Online

    June 7, 2026

    When to Upgrade from Knee Cap to Knee Brace Guide

    May 18, 2026

    Why Continuous Learning Has Become the Most Important Skill in Technology

    May 16, 2026

    The Future Of Dental Implants With A Skilled Implant Dentist In Sydney

    May 15, 2026

    THE INVERCARGILL RENTAL MARKET FOR 2026: AN INVESTOR’S GUIDE.

    May 10, 2026

    Enhancing Exterior Durability With Advanced Siding Materials

    May 9, 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

    Ironmartonline Reviews: Complete Guide to Buying Heavy Equipment Online

    June 7, 2026

    When to Upgrade from Knee Cap to Knee Brace Guide

    May 18, 2026
    Contact Us

    Phone: Whatsapp

    Mail: tech4links@gmail.com

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

    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