g = h.Graph()
g = h.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:
from neuron import h, gui
import math
# Create the graph
g = h.Graph()
# specify coordinate system for the canvas drawing area
# numbers are: xmin, xmax, ymin, ymax respectively
g.size(0, 10, -1, 1)
# the next g.line command will move the drawing pen to the
# indicated point without drawing anything
g.beginline()
# define a sine wave, 0 <= x <= 10
for i in xrange(101):
x = i * 0.1
g.line(x, math.sin(x))
# actually draw the plot on the graph in the window
g.flush()
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("label", _ref_variable)
g.addvar("label", _ref_variable, color_index, brush_index)
Add the variable to the list of items graphed when g.plot(x) is called. The address of the variable is used so this is fast. The current color and brush is used if the optional arguments are not present.
Additional syntaxes are available for plotting HOC variables.
Note
To automatically plot a variable added to a graph g with addvar against t during a run(), stdrun.hoc must be loaded (this is done automatically with a from neuron import gui) and the graph must be added to a graphList, such as by executing graphList[0].append(g).
Example:
g.addvar('Calcium', soma(0.5)._ref_cai)
Note
Not that useful in Python; only works with HOC expressions.
g.addexpr("HOC expression")
g.addexpr("HOC expression", color_index, brush_index)
g.addexpr("label", "HOC expr", object, ....)
Add a HOC 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:
from neuron import h, gui g = h.Graph() g.size(0, 10, -1, 1) g.addexpr("sin(x)") g.addexpr("cos(x)") g.addexpr("exp(-x)") # have to initialize the variable in HOC h("x = 0") g.begin() for i in xrange(101): h.x = i * 0.1 g.plot(h.x) g.flush() .. image:: ../images/graph-addexpr.png :align: center
g.addobject(rangevarplot)
g.addobject(rangevarplot, color, brush)
Note
Not that useful in Python since only works with Graph.addexpr() which uses HOC expressions.
Initialize the list of graph variables so the next g.plot(x) is the first point of each graph line.
See Graph.plot() for an example.
Note
Not that useful in Python since only works with Graph.addexpr() and Graph.xexpr() which use HOC expressions.
The abscissa value for each item in the list of graph lines. Usually used in a for loop.
See Graph.plot() for an example.
Note
Not that useful in Python since only works with HOC expressions.
g.xexpr("HOC expression")
g.xexpr("HOC expression", usepointer)
Example:
from neuron import h, gui # Assign "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 = h.Graph() # size the window to fit the graph g.size(-4, 4, -4, 4) # store 3*sin(t) as a function to be plotted in g graphs g.addexpr('3*sin(t)') # the next graph will be blue g.color(3) # store 3 * sin(2 * t) as a function to be plotted g.addexpr("3*sin(2*t)") # store 3*cos(t) as the x function to be plotted in g graphs # The two previous expressions become the y values g.xexpr('3*cos(t)') g.begin() for i in xrange(64): # h.t ranges from 0 to 6.3 \approx 2 * pi h.t = i * 0.1 g.plot(h.t) # actually draws the graph g.flush()plots a black circle of radius=3 and a blue infinity-like figure, spanning from x=-3 to x=3.
![]()
Warning
On Microsoft Windows, 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).
from neuron import h, gui
g = h.Graph()
g.size(0, 4000, -1, 1)
g.addexpr("cos(t/100)")
g.addexpr("cos(t/150)")
g.addexpr("cos(t/200)")
g.addexpr("cos(t/250)")
g.addexpr("cos(t/300)")
g.addexpr("cos(t/450)")
def pl():
g.erase()
g.begin()
for h.t in range(4000):
g.plot(h.t)
if (h.t % 10 == 0) :
g.fastflush()
h.doNotify()
g.flush()
h.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.
Description:
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.
Note
For plotting Vector objects, it is typically easier to use Vector.plot(), Vector.line(), and Vector.mark().
Note
A segmentation violation will result if n is greater than the vector size.
Example:
from neuron import h, gui import numpy num_elements = 629 x = h.Vector(num_elements) y = h.Vector(num_elements) # fill x with 0, 0.01, 0.02, etc x.indgen(0.01) # set y to the sin of x via numpy y.as_numpy()[:] = numpy.sin(x) # create the graph g = h.Graph() g.size(0, 6.28, -1, 1) g.vector(num_elements, x._ref_x[0], y._ref_x[0]) g.flush()![]()
To iterate over all the lines in a Graph use:
xvec = h.Vector()
yvec = h.Vector()
j = 0
i = -1
while (i = Graph[0].getline(i, xvec, yvec) != -1) and (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 = 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:
g = h.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.
#Creates an object reference "g" which will
#point to the graph object.
g = h.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.
x = 0
while (x<=10): #//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.
x=x+0.1
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:
g = h.Graph() g.beginline() t = 0 while (t<=2*PI+0.1): g.line(sin(t), cos(t)) t = t+0.1 g.flush()graphs a circle of radius=1, just as would the following code using g.plot():
g = h.Graph() t = 0 g.addexpr("sin(t)") g.xexpr("cos(t)") g.begin() t = 0 while (t<=2*PI+0.1): g.plot(t) t = t + 0.1 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:
g = h.Graph() g.exec_menu("Keep Lines")
Example:
from neuron import h, gui g = h.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:
g = h.Graph() g.menu_tool("mouse events", "p") def 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