简体   繁体   中英

How to set the custom marker in MATLAB figure legend

I have an image as follows.

在此处输入图片说明 In this images you can see 3 lines, these are actually 6 lines, one line in invisible just for showing markers and other line is smoothened version of original data points. Now the issue is that how do I get the markers in legend. In this case, you can see the figure legend consists of just line, not the markers. My code is as follows.

clc; clear all ;

colour_green = [12 195 82] ./ 255;
colour_lightgreen = [94 250 81] ./ 255;
colour_lightblue = [8 180 238] ./ 255;
colour_darkblue = [1 17 181] ./ 255;
colour_peach = [251 111 66] ./ 255;

figure('DefaultAxesFontSize',30);
set(0,'DefaultAxesFontName',' Times  ');
hold on 


time_window = xlsread('pattern_data.xlsx', 'effect_of_count', 'A2:A12');
count1plus = xlsread('pattern_data.xlsx', 'effect_of_count', 'B2:B12');
count10plus = xlsread('pattern_data.xlsx', 'effect_of_count', 'C2:C12');
count1to5 = xlsread('pattern_data.xlsx', 'effect_of_count', 'D2:D12');


 x1 = 50:1:99;
%  x1 = .01:.01:.5;


x2 = interp1(time_window,count1plus,x1, 'pchip') ;
x3 = interp1(time_window,count10plus,x1, 'pchip') ;
x4 = interp1(time_window,count1to5,x1, 'pchip') ;


   % count 1+
plot(x1,x2,'b--','DisplayName', 'Count_{A} = 1: and Count_{B} = 1:','LineWidth',3) 
plot(time_window,count1plus,'bs', 'HandleVisibility','off','LineWidth',5)

   % count 1:5
plot(x1,x4,'-','DisplayName', 'Count_{A} = 1: and Count_{B} = 5:','LineWidth',3 ,  'Color', colour_green) 
plot(time_window,count1to5,'^', 'HandleVisibility','off','LineWidth',5 ,  'Color', colour_green)

   % count 10+
plot(x1,x3,'r--','DisplayName', 'Count_{A} = 1: and Count_{B} = 10:','LineWidth',3) 
plot(time_window,count10plus,'ro', 'HandleVisibility','off','LineWidth',5)


hold off


xlabel('Th_{B} ')
ylabel('L (milliseconds)')
legend('Location','north')
legend show

set(gcf, 'PaperUnits', 'normalized');
set(gcf, 'PaperPosition', [0 0 1 1]);
set(gcf,'PaperOrientation','l');
print -dpng graphs/p1_effect_of_count_and_selB;

Please help. What I want is to have a marker(circle, square etc.) in the respective legend.

Edit# 1

The solution provided here did not solve my problem as it did not illustrates how to add a marker. An animation shows how to customize the position of a marker, but here I want to add a new marker not re-position the original existing one.

As explained in this answer to a very similar question , since R2014b onward the legend object is kind of opaque and cannot be easily changed. However, that answer also shows that there is a syntax to the legend function with four output arguments, which creates the legend in a different way such that it can be modified. The answer also shows how to modify the legend. We'll follow their lead.

The documentation says about this syntax:

This syntax is not recommended. It creates a legend that does not support some functionality, such as adding a legend title. Also, the legend does not automatically update when you add or remove data series from the axes.

But in this case these issues don't bother us, so we'll continue along.

In the case of the graph in the question, we'll replace

legend('Location','north')

with

[lgd,icons,plots,txt] = legend('Location','north');

Now, icons contains handles to the objects that form the legend:

>> icons

icons = 

  9×1 graphics array:

  Text    (Count_{A} = 1: and Count_{B} = 1:)
  Text    (Count_{A} = 1: and Count_{B} = 5:)
  Text    (Count_{A} = 1: and Count_{B} = 10:)
  Line    (Count_{A} = 1: and Count_{B} = 1:)
  Line    (Count_{A} = 1: and Count_{B} = 1:)
  Line    (Count_{A} = 1: and Count_{B} = 5:)
  Line    (Count_{A} = 1: and Count_{B} = 5:)
  Line    (Count_{A} = 1: and Count_{B} = 10:)
  Line    (Count_{A} = 1: and Count_{B} = 10:)

The display even helpfully shows which elements belong to which item. The first three are text objects, the last 6 are line objects. These line objects are what we need to modify.

Why are there two line objects for each of the items shown in the legend? That is because there is one line object (the first one) that is the rendered line (it has two data points):

>> icons(4)

ans = 

  Line (Count_{A} = 1: and Count_{B} = 1:) with properties:

              Color: [0 0 1]
          LineStyle: '--'
          LineWidth: 3
             Marker: 'none'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: [0.0108 0.0919]
              YData: [0.8246 0.8246]
              ZData: [1×0 double]

and the other that is the marker (currently not visible, it has one data point):

>> icons(5)

ans = 

  Line (Count_{A} = 1: and Count_{B} = 1:) with properties:

              Color: [0 0 1]
          LineStyle: 'none'
          LineWidth: 3
             Marker: 'o'
         MarkerSize: 6
    MarkerFaceColor: 'none'
              XData: 0.0514
              YData: 0.8246
              ZData: [1×0 double]

So, what we need to do is set these marker objects:

icons(5).Marker = 's';
icons(7).Marker = '^';
icons(9).Marker = 'o';

Now the plot looks like you wanted.

I hope that the description above is clear enough that you can now change the legend in other ways too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM