[37] | 1 | function labeled_curves(x,y,varargin)
|
---|
| 2 | %LABELED_CURVES Draw multiple curves in different colors with labels.
|
---|
| 3 | % LABELED_CURVES(X,Y) plots multiples curves in different colors, with
|
---|
| 4 | % a legend and good axes (using AXIS_PCT).
|
---|
| 5 | % There are multiple ways to specify X and Y.
|
---|
| 6 | %
|
---|
| 7 | % In the simplest case, every curve has the same x-coordinates, so X is
|
---|
| 8 | % just a vector and Y is a matrix. Y can also be a structure where each
|
---|
| 9 | % field is a vector of y-coordinates (of equal lengths).
|
---|
| 10 | %
|
---|
| 11 | % If curves have different x-coordinates then X should be a structure where
|
---|
| 12 | % each field is a vector of x-coordinates, and Y is a corresponding structure
|
---|
| 13 | % where each field is a vector of y-coordinates.
|
---|
| 14 | %
|
---|
| 15 | % LABELED_CURVES(X,Y,'color',COLOR) uses the given colors.
|
---|
| 16 | % COLOR is either a cell array of linespec strings (as in PLOT),
|
---|
| 17 | % a matrix of RGB triples (as in COLORMAP), or a structure where each field
|
---|
| 18 | % is either a linespec string or RGB triple.
|
---|
| 19 | %
|
---|
| 20 | % If you are making multiple plots, you can collect all the color
|
---|
| 21 | % specifications into a single color structure, which ensures the coloring
|
---|
| 22 | % of a particular curve is consistent among plots.
|
---|
| 23 | %
|
---|
| 24 | % By default, the structure field names are used as labels. Alternatively,
|
---|
| 25 | % you can specify labels as an optional argument:
|
---|
| 26 | %
|
---|
| 27 | % LABELED_CURVES(X,Y,...,'labels',LABELS) uses the given labels for the legend
|
---|
| 28 | % (LABELS is a cell array of strings). Otherwise the field names are used.
|
---|
| 29 | %
|
---|
| 30 | % LABELED_CURVES(X,Y,...,'plotfun',PLOTFUN) uses PLOTFUN instead of 'plot' to
|
---|
| 31 | % draw the curves. For example, 'semilogx', 'semilogy', or 'loglog'.
|
---|
| 32 | %
|
---|
| 33 | % LABELED_CURVES(X,Y,...,'mobile',1) uses MOBILE_TEXT instead of
|
---|
| 34 | % LEGEND, for more accurate placement of labels.
|
---|
| 35 | %
|
---|
| 36 | % Example:
|
---|
| 37 | % x = linspace(0,6,100);
|
---|
| 38 | % y = [sin(x); cos(x)];
|
---|
| 39 | % labeled_curves(x,y,'labels',{'sin' 'cos'})
|
---|
| 40 | % color = {'g' 'b'};
|
---|
| 41 | % labeled_curves(x,y,'color',color,'labels',{'sin' 'cos'})
|
---|
| 42 | % color = [0 1 0; 0 0 1];
|
---|
| 43 | % labeled_curves(x,y,'color',color,'labels',{'sin' 'cos'})
|
---|
| 44 | %
|
---|
| 45 | % y=struct;color=struct;
|
---|
| 46 | % y.sin = sin(x);
|
---|
| 47 | % y.cos = cos(x);
|
---|
| 48 | % color.sin = 'g';
|
---|
| 49 | % color.cos = 'b';
|
---|
| 50 | % labeled_curves(x,y,'color',color)
|
---|
| 51 | % labeled_curves(x,y,'color',color,'plotfun','semilogx','mobile',1)
|
---|
| 52 | %
|
---|
| 53 | % xcoord = struct;
|
---|
| 54 | % xcoord.sin = x;
|
---|
| 55 | % xcoord.cos = x;
|
---|
| 56 | % xcoord.tan = linspace(2,4,100);
|
---|
| 57 | % y.tan = tan(xcoord.tan);
|
---|
| 58 | % color.tan = [1 0 0];
|
---|
| 59 | % labeled_curves(xcoord,y,'color',color)
|
---|
| 60 | %
|
---|
| 61 | % See also PLOT, LEGEND, MOBILE_TEXT, AXIS_PCT, LINECHART
|
---|
| 62 |
|
---|
| 63 | % Written by Tom Minka
|
---|
| 64 | % (c) Microsoft Corporation. All rights reserved.
|
---|
| 65 |
|
---|
| 66 | args = makestruct(varargin);
|
---|
| 67 | default_args = struct('color',[],'labels',[],'plotfun','plot','mobile',0,'dotstyle','.');
|
---|
| 68 | args = setfields(default_args,args);
|
---|
| 69 | color = args.color;
|
---|
| 70 | labels = args.labels;
|
---|
| 71 | plotfun = args.plotfun;
|
---|
| 72 | mobile_flag = args.mobile;
|
---|
| 73 | dotstyle = args.dotstyle;
|
---|
| 74 |
|
---|
| 75 | if ~isstruct(y)
|
---|
| 76 | a = y;
|
---|
| 77 | y = struct;
|
---|
| 78 | for i = 1:rows(a)
|
---|
| 79 | y.(sprintf('V%d',i)) = a(i,:);
|
---|
| 80 | end
|
---|
| 81 | end
|
---|
| 82 | if isempty(color)
|
---|
| 83 | n = length(fieldnames(y));
|
---|
| 84 | color = jet(n);
|
---|
| 85 | color = hsv(n);
|
---|
| 86 | end
|
---|
| 87 | if ~isstruct(color)
|
---|
| 88 | a = color;
|
---|
| 89 | color = struct;
|
---|
| 90 | fields = fieldnames(y);
|
---|
| 91 | if iscellstr(a)
|
---|
| 92 | for i = 1:length(a)
|
---|
| 93 | color.(fields{i}) = a{i};
|
---|
| 94 | end
|
---|
| 95 | else
|
---|
| 96 | for i = 1:rows(a)
|
---|
| 97 | color.(fields{i}) = a(i,:);
|
---|
| 98 | end
|
---|
| 99 | end
|
---|
| 100 | end
|
---|
| 101 | if isempty(labels)
|
---|
| 102 | labels = fieldnames(y);
|
---|
| 103 | end
|
---|
| 104 |
|
---|
| 105 | lastx = struct;
|
---|
| 106 | lasty = struct;
|
---|
| 107 | h = [];
|
---|
| 108 | for f = fieldnames(y)'
|
---|
| 109 | field = char(f);
|
---|
| 110 | if isstruct(x)
|
---|
| 111 | thisx = x.(field);
|
---|
| 112 | else
|
---|
| 113 | thisx = x;
|
---|
| 114 | end
|
---|
| 115 | thisy = y.(field);
|
---|
| 116 | thiscolor = color.(field);
|
---|
| 117 | if ischar(thiscolor)
|
---|
| 118 | h(end+1) = feval(plotfun,thisx,thisy,thiscolor);
|
---|
| 119 | hold on
|
---|
| 120 | if ~isempty(dotstyle)
|
---|
| 121 | feval(plotfun,thisx,thisy,[thiscolor dotstyle]);
|
---|
| 122 | end
|
---|
| 123 | else
|
---|
| 124 | h(end+1) = feval(plotfun,thisx,thisy);
|
---|
| 125 | set(h(end),'Color',thiscolor);
|
---|
| 126 | hold on
|
---|
| 127 | if ~isempty(dotstyle)
|
---|
| 128 | hh = feval(plotfun,thisx,thisy,dotstyle);
|
---|
| 129 | set(hh,'Color',thiscolor);
|
---|
| 130 | end
|
---|
| 131 | end
|
---|
| 132 | lastx.(field) = thisx(end);
|
---|
| 133 | lasty.(field) = thisy(end);
|
---|
| 134 | end
|
---|
| 135 | hold off
|
---|
| 136 | axis_pct;
|
---|
| 137 | f = fieldnames(y);
|
---|
| 138 | legend(h,labels)
|
---|
| 139 | if mobile_flag
|
---|
| 140 | legend off
|
---|
| 141 | h = mobile_text(labels{:});
|
---|
| 142 | for i = 1:length(h)
|
---|
| 143 | set(h(i),'Position',[lastx.(f{i}) lasty.(f{i})]);
|
---|
| 144 | end
|
---|
| 145 | end
|
---|