1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2004-2006 - INRIA - Fabrice Leray
3 // Copyright (C) 2008 - INRIA - Jean-Baptiste Silvy
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution. The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 function plot(varargin)
11 // Try to build a new better parser that could manage things like:
12 // plot(x,y,'X',1:10); // where X stands for Xdata (Matlab recognize
13 //it and treats it well...
18 //LineSpec and PropertySpec examples:
23 plot(t,sin(t),"ro-.",t,cos(t),"cya+",t,abs(sin(t)),"--mo");
25 plot([t ;t],[sin(t) ;cos(t)],"xdat",[1:2]);
32 CurColor = 0; // current color used if no color specified via LineSpec
39 //detect and set the current axes now:
40 if type(ListArg(1)) == 9
42 if (hdle.type == "Axes")
44 ListArg(1) = null(); // remove this parameter from the list
46 warning("Handle should be an Axes handle")
62 argTypes(curArgIndex,1) = type(ListArg(curArgIndex))
69 acceptedTypes=find(Ttmp(i,1)==1 & or(Ttmp(i+1,1)==[1,13,130])) // to accept double, macro function or primitive as second argument
71 if (acceptedTypes<>[]) then
73 Ttmp(i,1) = 99; // Replace a known type by 99 (no meaning) to count it once only!
74 Ttmp(i+1,1)= 99; // to avoid having (x1,y1,x2,y2) ->couple=[1,2,3]
75 // With this trick, couple=[1,3];
81 if (couple==[]) // No data couple found
82 // Search for at least a single data , i.e.: plot(y)
84 if ((argTypes(1,1)==1 | argTypes(1,1)==8) & ListArg(1)<>[]) then // case plot(SINGLE y,...)
88 if (modulo(nv-couple,2)<>0) then
89 P1 = couple+2 // Position of the first PropertyName field
95 warning("Error inside input argument : no data");
101 // Some test to check wrong inputs
103 // 1. Test if 2 data couples (first : type==1, second : type=[1,13,130])
104 // are at least separated by 2 indices
105 if (couple(2:$)-couple(1:$-1)<2)
106 warning("Error inside input argument !");
110 // 2. Test if no string couples happen before P1 (see below for P1 definition)
111 for index=1:couple($)
113 acceptedTypes=find(Ttmp(index,1)==10 & Ttmp(index+1,1)==10)
115 if (acceptedTypes<>[]) then
116 warning("Error inside input argument : String argument is an unknown option.");
123 if (modulo(nv-(couple($)+1),2)<>0) then
124 P1 = couple($)+3 // Position of the first PropertyName field
131 numplot = size(couple,"*");
133 xyIndexLineSpec = zeros(numplot,3);
134 // xyIndexLineSpec is a matrix storing the index of x, y and linespec
135 // if one of these indices is 0 => it does not exist
136 // (which is possible for x and linepsec, not for y)
138 if (provided_data == 2) then
140 for curCouple=1:size(couple,"*")
141 xyIndexLineSpec(curCouple,1:2) = couple(curCouple) +[0,1] // x,y index storage
143 if (couple(curCouple)+2 < P1)
144 if (argTypes(couple(curCouple)+2,1)==10) then // LineSpec treatment
145 xyIndexLineSpec(curCouple,3) = couple(curCouple)+2;
150 // we are in the case where: plot(SINGLE y,... x not specified
151 // or plot(handle,SINGLE y,...
152 xyIndexLineSpec(1,1) = 0; // no x specified
153 xyIndexLineSpec(1,2) = couple;
158 if (argTypes(couple+1,1)==10) then // LineSpec treatment
159 xyIndexLineSpec(1,3) = couple+1;
166 // delay the drawing commands
168 current_figure=gcf();
169 cur_draw_mode = current_figure.immediate_drawing;
170 current_figure.immediate_drawing = "off";
172 // check wether this is the first plot for the axes in which we will draw
174 // save auto_clear state.
175 OldAutoClear = curAxes.auto_clear;
177 isFirstPlot = (curAxes.children == [])
179 //Now, we plot the decomposed plots one by one with their own linespec
180 // provided_data = 2 : x and y are provided
182 FinalAgreg=[]; // Final Compound containing all the new created plots.
186 // Set off auto_clear for allowing multiple graphics entity
187 // will be restored behond
189 curAxes.auto_clear="off";
200 if (provided_data == 2) then
202 if (type(ListArg(xyIndexLineSpec(i,2))) == 13 | type(ListArg(xyIndexLineSpec(i,2))) == 130)
203 // A function (macro or primitive) is given. We need to build the vector or matrix.
204 sizefirstarg = size(ListArg(xyIndexLineSpec(i,1)));
205 buildFunc = ListArg(xyIndexLineSpec(i,2));
206 firstarg = ListArg(xyIndexLineSpec(i,1));
209 for ii=1:sizefirstarg(1,2)
210 for jj=1:sizefirstarg(1,1)
212 // function evaluation may fail
213 // try/cacth is buggy for now
214 // so use execstr until the bug is fixed
215 err = execstr("tmp(jj,ii) = buildFunc(firstarg(jj,ii))","errcatch","n");
219 ResetFigureDDM(current_figure, cur_draw_mode);
222 [err_message, err_number, err_line, err_func] = lasterror(%t);
226 if (err_func <> "") then
228 error(msprintf(gettext("%s: Error : unable to evaluate input function ''%s''.") + ascii(10) + gettext("Error %d at line %d of the function: ''%s''"), "plot", err_func,err_number, err_line, err_message));
230 error(msprintf(gettext("%s: Error : unable to evaluate input function.") + ascii(10) + gettext("Error %d at line %d of the function: ''%s''"), "plot", err_number, err_line, err_message));
240 ListArg(xyIndexLineSpec(i,2)) = tmp;
241 // if there is an other iteration, we will have error message redefining function.
242 // we need to clear here and not before, because user must see the warning if needed.
245 [X,Y] = checkXYPair(typeOfPlot,ListArg(xyIndexLineSpec(i,1)),ListArg(xyIndexLineSpec(i,2)),current_figure,cur_draw_mode)
247 if or(size(ListArg(xyIndexLineSpec(1,2)))==1) // If this is a vector
248 X=1:length(ListArg(xyIndexLineSpec(1,2))); // insert an abcsissa vector of same length,
249 else // if this is a matrix,
250 X=1:size(ListArg(xyIndexLineSpec(1,2)),1); // insert an abcsissa vector with
252 [X,Y] = checkXYPair(typeOfPlot,X,ListArg(xyIndexLineSpec(1,2)),current_figure,cur_draw_mode)
255 // Case if 'Xdata', 'Ydata' or 'Zdata' have been set in (PropertyName,Propertyvalue) couples
256 // must be taken into account now
258 // P1 is the position of the first PropertyName field.
261 while (Property <= nv-1)
262 PropertyName = ListArg(Property);
263 PropertyValue = ListArg(Property+1);
265 // Xdata can ONLY be a vector (cf. Matlab help)
266 PName = getPlotPropertyName(PropertyName,current_figure,cur_draw_mode);
267 if (PName == "xdata")
269 if (type(PropertyValue)<>1 | and(size(PropertyValue)<>1))
270 warning("Xdata value must be a column or row vector.");
271 ResetFigureDDM(current_figure, cur_draw_mode);
274 PropertyValue = PropertyValue(:); // force
275 if or(size(X))==1 // If X is a vector (inevitably a column vector because checkXYPair always returns a column vector)
276 X = PropertyValue; // X is replaced by PropertyValue
277 [X,Y] = checkXYPair(typeOfPlot,X,Y,current_figure,cur_draw_mode)
278 else // X is a matrix
279 if size(PropertyValue,"*") == size(X,1)
280 for j=1:size(PropertyValue,"*")
281 X(j,:) = PropertyValue(j,1);
284 str="plot : incompatible dimensions in input arguments";
286 ResetFigureDDM(current_figure, cur_draw_mode);
291 // Ydata ONLY be a vector (contrary to what is said by the Matlab help)
292 elseif (PName == "ydata")
294 if (type(PropertyValue)<>1 | and(size(PropertyValue)<>1))
295 warning("Ydata value must be a column or row vector.");
296 ResetFigureDDM(current_figure, cur_draw_mode);
299 PropertyValue = PropertyValue(:); // force
300 if or(size(Y))==1 // If Y is a vector (inevitably a column vector because checkXYPair always returns a column vector)
301 Y = PropertyValue; // Y is replaced by PropertyValue
302 [X,Y] = checkXYPair(typeOfPlot,X,Y,current_figure,cur_draw_mode)
303 else // Y is a matrix
304 if size(PropertyValue,"*") == size(Y,1)
305 for j=1:size(PropertyValue,"*")
306 Y(j,:) = PropertyValue(j);
309 str="plot : incompatible dimensions in input arguments";
311 ResetFigureDDM(current_figure, cur_draw_mode);
317 // Zdata will be treated after plot building
320 Property = Property+2;
325 //Now we have an array xyIndexLineSpec [numplot x 3] containing indices pointing on T for :
326 // - x (<>0 if existing)
328 // - linespec (<>0 if existing)
329 // for each plot passed in argument
332 //plot1 0|i1 |0 <=> plot(y)
333 //plot2 i2|i3 |0 <=> plot(x,y)
334 //plot3 i4|i5 |i6 <=> plot(x,y,LINESPEC)
339 if (xyIndexLineSpec(i,3)<>0) then // if we have a line spec <=> index <> 0
340 [Color,Line,LineStyle,Marker,MarkerStyle,MarkerSize,fail] = getLineSpec(ListArg(xyIndexLineSpec(i,3)),current_figure,cur_draw_mode);
343 // The plot is made now :
344 err = execstr("plot2d(X,Y)","errcatch","m");
347 mprintf("Error %d : in plot2d called by plot",err);
348 ResetFigureDDM(current_figure, cur_draw_mode);
352 agreg=gce(); // when using plot2d, we always have an Compound as the current entity
354 FinalAgreg = [agreg FinalAgreg];
362 for ii=size(agreg.children,"*"):-1:1
363 curPolyline=agreg.children(ii); // we apply linespec to the lines
365 // Color treatment : if no color specified by LineSpec nor PropertyName
366 // Set the default color to the curve
367 if DefaultColor == %T
368 [Color,CurColor] = setDefaultColor(CurColor);
372 curPolyline.mark_style=MarkerStyle;
373 curPolyline.mark_mode ="on";
374 curPolyline.mark_foreground = Color;
375 curPolyline.mark_style=MarkerStyle;
376 curPolyline.mark_size=MarkerSize;
378 curPolyline.mark_mode ="off"
382 curPolyline.line_mode="on";
383 curPolyline.foreground = Color;
384 curPolyline.line_style = LineStyle;
386 curPolyline.line_mode="off"
389 if (Line == %F & Marker ==%F) // no linespec nor PropertyName set
390 curPolyline.line_mode="on";
391 curPolyline.foreground = Color;
392 curPolyline.line_style = LineStyle;
398 //Reset auto_clear Property
399 curAxes.auto_clear = OldAutoClear;
401 ///////////////////////////////////
402 //Global Property treatment //
403 //PropertyName and PropertyValue //
404 ///////////////////////////////////
408 // Those properties will be applied to Agreg children
409 Agreg = glue(FinalAgreg(1:$))
411 nbCompound = find(Agreg.children.type=="Compound")
413 while (nbCompound<>[])
414 nbCompound=nbCompound(1);
415 unglue(Agreg.children(nbCompound));
416 nbCompound=find(Agreg.children.type=="Compound")
421 // P1 is the position of the first PropertyName field.
424 Curves = Agreg.children
425 //Curves(:,1) = Curves(:,$:-1:1);
427 // set mark_size_unit to 'point' for all the curves
428 Curves.mark_size_unit="point";
430 while (Property <= nv-1)
431 setPlotProperty(ListArg(Property),ListArg(Property+1),Curves,current_figure,cur_draw_mode)
433 Property = Property+2;
436 // force drawing of box like in matlab
438 // unless we are using centered axes
439 // to keep compatibility with Scilab 4
440 if isFirstPlot & curAxes.x_location <> "origin" & curAxes.y_location <> "origin" then
446 //postponed drawings are done now !
448 ResetFigureDDM(current_figure, cur_draw_mode)