g = new Graph()
g = new Graph(0)
Description:
An instance of the Graph class manages a window on which x-y plots can be drawn by calling various member functions. The first form immediately maps the window to the screen. With a 0 argument the window is not mapped but can be sized and placed with the view() function.
The most basic interpreter prototype for producing a plot follows:
objref g //Creates an object reference "g" which can be made to
//point to any object.
g = new Graph() //Assigns "g" the role of pointing to a Graph instance
//created from the Graph class, and produces
//a graph window with x and y axes on the
//screen.
g.size(0, 10, -1, 1) // specify coordinate system for the canvas drawing area
// numbers refer to xmin, xmax, ymin, ymax respectively
g.beginline() //The next g.line command will move the drawing pen
// to the indicated point without drawing anything
for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted
//will go from 0 to 10 in increments
//of 0.1.
g.line(x, sin(x)) //States that the y values on the plot
//will be the sin of the x values.
}
g.flush() //Actually draws the plot on the graph in the window.
The function .line(), however, only allows the user to plot one function per for loop, whereas the function .plot() can produce several plots per for loop and is therefore more effective in comparing plots. You must use .begin() and .addvar() or .addexpr() in conjunction with the .plot function.
objref g
g = new Graph()
g.size(0, 10, -1, 1)
g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g
g.addexpr("cos(x)") //stores cos(x) for use with g
g.addexpr("exp(-x)") //stores exp(x) for use with g
x=0
g.begin() //The next g.plot command will move the drawing pens
// for the three curves to indicated x position
for(x=0; x<=10; x=x+0.1){
g.plot(x) // The x value used for each expression in the
// addexpr list
}
g.flush()
The size in the above example is appropriate to show the sine waves nicely but the view of the exponential only shows the first few points before it goes out of view. Hold the right mouse button while the mouse in in the graph window and select the "View = plot" menu item to see the entire exponential. Selecting the "Whole Scene" menu item will return to the size specified in the size command.
g.xaxis()
g.xaxis(mode)
g.xaxis(xstart, xstop)
g.xaxis(xstart, xstop, ypos, ntic, nminor, invert, shownumbers)
g.yaxis()
g.yaxis(mode)
g.yaxis(ystart, ystop)
g.yaxis(ystart, ystop, ypos, ntic, nminor, invert, shownumbers)
The single mode argument draws both x and y axes (no arg == mode 0).
The xpos argument gives the location of the yaxis on the xaxis (default 0).
shownumbers=0 will not draw the axis labels.
invert=1 will invert the axes.
Note:
It is easiest to control the size of the axes and the scale of the graph through the graphical user interface. Normally, when a new graph is declared (eg. g = new Graph()), the y axis ranges from 20-180 and the x axis ranges from 50-250. With the mouse arrow on the graph window, click on the right button and set the arrow on View at the top of the button window column. A second button window will appear to the right of the first, and from this button window you can select several options. Two of the most common are:
Size the window to best-fit the plot which it contains.
Allows you to click on the left mouse button and perform the following tasks:
scale down the x axis (eg. 50 - 250 becomes 100 - 110)
view parts of the axis which are to the left of the original window
scale up the x axis (eg. 50 - 250 becomes -100 - 500)
view parts of the axis which are to the right of the original window
scale down the y axis (eg. 20 - 180 becomes 57.5 - 62)
view parts of the axis which are below the original window
scale up the y axis (eg. 20 - 180 becomes -10,000 - 5,000)
view parts of the axis which are above the original window
You can also use the size command to determine the size of what you view in the graph window. Eg. g.size(-1,1,-1,1) makes both axes go from -1 to 1.
g.addvar("variable")
g.addvar("variable", color_index, brush_index)
g.addvar("label", "variable")
g.addvar("label", "variable", color_index, brush_index)
g.addvar("label", &variable, ...)
Add the variable to the list of items graphed when g.plot(x) is called. The address of the variable is computed so this is fast. The current color and brush is used if the optional arguments are not present. The name of the variable is also added to the graph as a label associated with the line. If the first two args are strings, then the first "label" arg is associated with the line on the graph whereas the second arg defines the variable.
The second arg may be an explicit pointer arg which allows g.addvar to be used in Python using section(x)._ref_rangevar .
Note
To automatically plot a variable added to a graph g with addvar against t during a run(), stdrun.hoc must be loaded and the graph must be added to a graphList, such as by executing graphList[0].append(g).
g.addexpr("expression")
g.addexpr("expression", color_index, brush_index)
g.addexpr("label", "expr", object, ....)
Add an expression (eg. sin(x), cos(x), exp(x)) to the list of items graphed when g.plot(x) is called.
The current color and brush is used if the optional arguments are not present. A label is also added to the graph that indicates the name of the variable. The expression is interpreted every time g.plot(x) is called so it is more general than addvar(), but slower.
If the optional label is present that string will appear as the label instead of the expr string. If the optional object is present the expr will be evaluated in the context of that object.
Example:
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph g.size(0,10,-1,1) //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("exp(-x)") //stores exp(x) for use with g x=0 // has to be defined prior to execution of expressions g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
g.addobject(rangevarplot)
g.addobject(rangevarplot, color, brush)
Example:
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("-exp(x)") //stores exp(x) for use with g x=0 g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
Example:
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.addexpr("sin(x)") //stores sin(x) as a function to be plotted in g graphs g.addexpr("cos(x)") //stores cos(x) for use with g g.addexpr("cos(2*x)") //stores cos(2*x) for use with g x=0 g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(x) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.
g.xexpr("expression")
g.xexpr("expression", usepointer)
Example:
objref g //Creates an object reference "g" which will //point to the graph object. g = new Graph() //Assigns "g" the role of pointing to a Graph //created from the Graph class, and produces //a graph window with x and y axes on the //screen. g.size(-4,4,-4,4) //sizes the window to fit the graph t = 0 //Declares t as a possible variable g.addexpr("3*sin(t)") //stores 3*sin(t) as a function to be plotted in g graphs g.color(3) //the next graph will be drawn in blue g.addexpr("3*sin(2*t)") //stores 3*sin(2*t) as a function to be plotted g.xexpr("3*cos(t)") //stores 3*cos(t) as the x function to be plotted in g graphs //sin(x) becomes the y function g.begin() //Tells the interpreter that commands to plot //specific functions will follow. for(t=0; t<=2*PI+0.1; t=t+0.1){ //States that x values to be plotted //will go from 0 to 10 in increments //of 0.1. g.plot(t) //States that the y values on the plot //will be the sin of the x values. } g.flush() //Actually draws the plot on the graph in the window.plots a black circle of radius=3 and a blue infinity-like figure, spanning from x=-3 to x=3.
Warning
Because Microsoft Windows is a second-class operating system, too many points, too close together will not appear at all on a graph window. You can, in such a case, zoom in to view selected parts of the function.
Flushes only the plot() (x) points since the last flush() (or fastflush). This is useful for seeing the progress of addvar() plots during long computations in which the graphlines contain many thousands of points. Make sure you do a normal .flush when the lines are complete since fastflush does not notify the system of the true size of the lines. In such cases, zooming, translation, and crosshairs do not always work properly till after the flush() command has been given. (Note, this is most useful for time plots).
objectvar g
g = new Graph()
g.size(0,4000, -1,1)
g.addexpr("cos(x/100)")
g.addexpr("cos(x/150)")
g.addexpr("cos(x/200)")
g.addexpr("cos(x/250)")
g.addexpr("cos(x/300)")
g.addexpr("cos(x/450)")
proc pl() {
g.erase()
g.begin()
for (x=0; x < 4000; x=x+1) {
g.plot(x)
if (x%10 == 0) {
g.fastflush()
doNotify()
}
}
g.flush()
doNotify()
}
pl()
g.family(boolean)
g.family("varname")
The first form is similar to the Keep Lines item in the graph menu of the graphical user interface.
With a string argument which is a variable name, the string is printed as a label and when keep lines is selected each line is labeled with the value of the variable.
When graphs are printed to a file in Ascii mode, the lines are labeled with these labels. If every line has a label and each line has the same size, then the file is printed in matrix form.
.vector(n, &x[0], &y[0])
.vector("namey")
Description:
- .vector(n, &x[0], &y[0])
Rudimentary graphing of a y-vector vs. a fixed x-vector. The y-vector is reread on each .flush() (x-vector is not reread). Cannot save and cannot keep lines.
Notes:
These vectors are assumed to be doubles and not vectors from the Vector class. The Vector class has its own functions Vector.plot(), Vector.line(), Vector.mark() for graphing vectors constructed in that class.
A segmentation violation will result if n is greater than the vector size.
- .vector("namey")
- equivalent to .vector(n, ..., &namey[0]) above with the advantage that it is saved in a session (because the symbol name is known). It is simpler in that the size n is obtained from the symbol but the plot is vs. the index of the vector. Not implemented.
To iterate over all the lines in a Graph use:
objref xvec, yvec
xvec = new Vector()
yvec = new Vector()
for (j=0 i=-1; (i = Graph[0].getline(i, xvec, yvec) != -1 ; j+=1 ) {
// xvec and yvec contain the line with Graph internal index i.
// and can be associated with the sequential index j.
print j, i, yvec.label
xline[j] = xvec.c
yline[j] = yvec.cl // clone label as well
}
g.size(xstart, xstop, ystart, ystop)
g.size(1-4)
g.size(&dbl[0])
Description:
- .size(xstart, xstop, ystart, ystop)
- The natural size of the scene in model coordinates. The "Whole Scene" menu item in the graphical user interface will change the view to this size. Default axes are this size.
- .size(1-4)
- Returns left, right, bottom or top of first view of the scene. Useful for programming.
- .size(&dbl[0])
- Returns the xmin, xmax, ymin, ymax values of all marks and lines of more than two points in the graph in dbl[0],..., dbl[3] respectively. This allows convenient computation of a view size which will display everything on the graph. See View_equal_Plot. In the absence of any graphics, it gives the size as in the .size(1-4) prototype.
.label(x, y, "label")
.label(x, y)
.label("label")
.label(x, y, "string", fixtype, scale, x_align, y_align, color)
Description:
- .label(x, y, "label")
- Draw a label at indicated position with current color.
- .label("label")
- Add a label one line below the previous label
- .label(x, y)
- Next label("string") will be printed at this location
The many arg form is used by sessions to completely specify an individual label.
Example:
objref g g = new Graph() g.align(0, 0) g.label(.5,.5, "left bottom at (.5,.5)") g.align(0, 1) g.label(.5,.5, "left top at (.5,.5)") g.align(1, 0) g.label(.5,.5, "right bottom at (.5,.5)") g.align(.5,2) g.label(.5,.5, "middle but twice height at (.5, .5)")
.color(index)
.color(index, "colorname")
Set the default color (starts at 1 == black). The default color palette is:
0 white
1 black
2 red
3 blue
4 green
5 orange
6 brown
7 violet
8 yellow
9 gray
The user may also use the colors/brushes button in the graphical user interface, which is called by placing the mouse arrow in the graph window and pressing the right button.
.brush(index)
.brush(index, pattern, width)
Description:
- .brush(index)
- Set the default brush. 0 is the thinnest line possible, 1-4 are thickness in pixel. Higher indices cycle through these line thicknesses with different brush patterns.
- .brush(index, pattern, width)
- Install a brush in the Brush Palette to be accessed with the index. The width is in pixel coords (< 1000). The pattern is a 31 bit pattern of 1's and 0's which is used to make dash patterns. Fractional widths work with postscript but not idraw. Axes are drawn with the nrn.defaults property *default_brush: 0.0
The user may also use the ChangeColor-Brush button in the graphical user interface, which is called by placing the mouse arrow in the graph window and pressing the right button.
.view(mleft, mbottom, mwidth, mheight, wleft,
wtop, wwidth, wheight)
.view(2)
Map a view of the Shape scene. m stands for model coordinates within the window, w stands for screen coordinates for placement and size of the window. The placement of the window with respect to the screen is intended to be precise and is with respect to pixel coordinates where 0,0 is the top left corner of the screen.
The single argument form maps a view in which the aspect ratio between x and y axes is always 1. eg like a shape window.
.save_name("objectvar")
.save_name("objectvar", 1)
.beginline()
.beginline(color_index, brush_index)
.beginline("label")
.beginline("label", color, brush)
Notice that the argument to g.line() is the expression sin(x) itself, whereas if you were using the .plot() command, the arguments would have to be specified before the for loop using .addexpr() commands. The addexpr/begin/plot method of plotting is preferred since it is capable of simultaneously plotting multiple lines.
objref g //Creates an object reference "g" which will
//point to the graph object.
g = new Graph() //Assigns "g" the role of pointing to a Graph
//created from the Graph class, and produces
//a graph window with x and y axes on the
//screen.
g.beginline() //Tells the interpreter that commands to create a line for
//specific functions will follow.
for(x=0; x<=10; x=x+0.1){ //States that x values to be plotted
//will go from 0 to 10 in increments
//of 0.1.
g.line(x, sin(x)) //States that the y values on the line
//will be the sin of the x values.
}
g.flush() //Actually draws the plot on the graph in the window.
Draw a line from the previous point to this point. This command is normally used inside of a for loop. It is analogous to .plot() and the commands which go along with it. In the case of .line() however, all arguments are given in the line command itself. Therefore, the line command only plots one line at a time, whereas the .plot*() command can plot several lines using the same for loop on the same graph.
This command takes arguments for both x and y values, so it can serve the same purpose of the .plot command in conjunction with an .addexpr() command and an .xexpr() command.
Example:
objref g g = new Graph() g.beginline() for(t=0; t<=2*PI+0.1; t=t+0.1){ g.line(sin(t), cos(t)) } g.flush()graphs a circle of radius=1, just as would the following code using g.plot():
objref g g = new Graph() t = 0 g.addexpr("sin(t)") g.xexpr("cos(t)") g.begin() for(t=0; t<=2*PI+0.1; t=t+0.1){ g.plot(t) } g.flush()Note that the arguments to g.line are doubles, and not chars as they are in g.plot().
.mark(x, y)
.mark(x, y, "style")
.mark(x, y, "style", size)
.mark(x, y, "style", size, color, brush)
.crosshair_action("procedure_name")
.crosshair_action("procedure_name", vectorflag=0)
.crosshair_action("")
While the crosshair is visible (left mouse button pressed) one can type any key and the procedure will be executed with three arguments added: procedure_name(x, y, c) where x and y are the coordinates of the crosshair (in model coordinates) and c is the ascii code for the key pressed.
The procedure will be executed in the context of the object where crosshair_action was executed. When the optional vectorflag argument is 1, then, just prior to each call of the procedure_name due to a keypress, two temporary objectref's are created and assigned to a new Vector() and the line coordinate data is copied to those Vectors. With this form the call to the procedure has two args added: procedure_name(i, c, $o3, $o4) where i is the index of the crosshair into the Vector.
If you wish the Vector data to persist then you can assign to another objectvar before returning from the procedure_name. Note that one can copy any line to a Vector with this method whereas the interpreter controlled Graph.dump("expr", y_objectref) is limited to the current graphline of an addvar or addexpr.
With an empty string arg, the existing action is removed.
See also
Example:
objref g g = new Graph() g.exec_menu("Keep Lines")
Example:
objref g g = new Graph() g.menu_action("Print File", "g.printfile(\"temp.eps\") system(\"lp temp.eps\")")
.menu_tool("label", "procedure_name")
.menu_tool("label", "procedure_name", "select_action")
Add a selectable tool menu item to the Graph popup menu or else, if an xpanel() is open, an xradiobutton() will be added to the panel having the same action. (note: all menu_tool radiobuttons whether in the graph menu or in a panel, are in the same telltalegroup, so selecting one deselects the previous selection.)
If the third arg exists, the select_action will be executed when the radioitem is pressed (if it is not already selected).
When selected, the item will be marked and the label will appear on the window title bar (but not if the Graph is enclosed in a VBox() ). When this tool is selected, pressing the left mouse button, dragging the mouse, and releasing the left button, will cause procedure_name to be called with four arguments: type, x, y, keystate. x and y are the scene (model) coordinates of the mouse pointer, and type is 2 for press, 1 for dragging, and 3 for release. Keystate reflects the state of control (bit 1), shift (bit 2), and meta (bit 3) keys, ie control and shift down has a value of 3.
The rate of calls for dragging is of course dependent on the time it takes to execute the procedure name.
Example:
objref g g = new Graph() g.menu_tool("mouse events", "p") proc p() { print $1, $2, $3, $4 }
g.gif("file.gif")
g.gif("file.gif", left, bottom, width, height)
Suppose we have a gif with pixel width and height, wg and hg respectively. Also suppose we want the gif pixel point (xg0, yg0) mapped to graph model coordinate (x0, y0) and the gif pixel point (xg1, yg1) mapped to graph model coordinate (x1, y1). Then the last four arguments to g.gif should be:
left = x0 - xg0*(x1-x0)/(xg1-xg0)
bottom = y0 - yg0*(y1-y0)/(yg1-yg0)
width = wg*(x1-x0)/(xg1-xg0)
height= hg*(y1-y0)/(yg1-yg0)
If, for example with xv, you have constructed a desired rectangle on the gif and the info (xv controls/Windows/Image Info)presented is Resolution: 377x420 Selection: 225x279 rectangle starting at 135,44 then use
{wg=377 hg=420}
{xg0=135 yg0=420-(279+44) xg1=135+225 yg1=420-44}
Warning
In the single arg form, if the gif size is larger than the graph model coodinates, the graph is resized to the size of the gif. This prevents excessive use of memory and computation time when the graph size is on the order of a gif pixel.
i = g.view_info()
val = g.view_info(i, case)
val = g.view_info(i, case, model_coord)
Description:
Return information about the ith view.
With no args the return value is the view number where the mouse is. If the mouse was not last in a view of g, the return value is -1. Therefore this no arg function call should only be made on a mouse down event and saved for handling the other mouse events. Note that the two arg cases are generally constant between a mouse down and up event.
case 1: // width case 2: // height case 3: // point width case 4: // point height case 5: // left case 6: // right case 7: // bottom case 8: // top case 9: // model x distance for one point case 10: // model y distance for one point The following cases (11 - 14) require a third argument relative location means (0,0) is lower left and (1,1) is upper right. case 11: // relative x location (from x model coord) case 12: // relative y location (from y model coord) case 13: // points from left (from x model coord) case 14: // points from top (from y model coord) Note: this last is from the top, not from the bottom. case 15: // height of font in points