Subsections

11 Plotting 1D functions

When the canvas is ready, one can plot a 1D function using the :

>>  from jhplot import *
>>> c1=HPlot("Canvas")
>>> c1.visible(1)
>>> c1.setAutoRange()
>>> f1=F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
>>> c1.draw(f1)

Obviously, -2.0 and 5.0 specifies the range for the abscissa. You can find all the methods associated with this class as explained in Section. :autorefsection10. It should be noted that "draw" method can also take arrays of F1D functions.

Assuming that you have already imported the Color class from AWT, one can draw this function using various colors. Also, one can define the width for the line:

>>> f1=F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
>>> f1.setColor(Color.green)
>>> f1.setPenWidth(2)
>>> c1.draw(f1);

To draw dashed lines, use "setPenDash()" method. You can change the dashed lines by specifying an integer argument from 0 to 40. You can also use the ``update'' method to redraw the the plot:

11.1 How to use JAIDA to define 1D function

This is rather simple. Assume you have created IFunction using JAIDA. Then simple do:

>>> f1=F1D(m_IFunction, -2.0, 5.0)
>>> c1.draw(f1);

where ``m_IFunction'' is a variable of type IFunction. See the example `` histo_fit.py''.

11.2 Plotting 1D functions on the same plot

When you plot several functions on the same canvas, just simply use this:

>>> f1=F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
>>> f2=F1D("exp(-x*x/10)+cos(pi*x)/x", -2.0, 5.0)
>>> f1.setColor(Color.green); f1.setColor(Color.red)
>>> c1.draw(f1); c1.draw(f2)

11.3 Plotting 1D functions on different plots

Assume you want to plot two or more functions on different plots. In this case you have to create appropriate canvas, and the to change the directory using the ``cd'' method when you start to draw the function:

>>> from java.awt import Color
>>> from jhplot  import HPlot
>>> from jhplot  import F1D
>>> c1=HPlot("Canvas",600,400,1,2)
>>> c1.visible(1)
>>> c1.setAutoRange()
>>> f1=F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
>>> f2=F1D("exp(-x*x/10)+cos(pi*x)/x", -2.0, 5.0)
>>> f1.setColor(Color.green); f1.setColor(Color.red)
>>> c1.draw(f1)
>>> c1.cd(1,2)         # go to the second plot
>>> c1.draw(f2)

You can learn about the public methods of H1D by typing ``c1. <Ctrl>+<Space>'' Here are the major methods associated with the HPlot class:

>>> from jhplot  import HPlot
>>> c1=HPlot("Canvas")        # empty canvas
>>> c1.visible(1)             $ make it visible
>>> c1.setAutoRange()         # set autorange
>>> c1.cd(int, int)           # go to a specific region for plotting
>>> c1.update()               # update a plot on the region defined by the ``cd'' method
>>> c1.updateAll()            # update all the plots
>>> c1.drawStatBox( H1D )     # draw statistical box for 1HD histogram
>>> c1.setMargineTitle(int)   # define the region size for the global title
>>> c1.showMargineTitle(boolean b)    # do not show the global title (1: show the title)
>>> c1.setGTitle(String sname, Font f, Color c) # set the global title with Font and Color
>>> c1.viewHisto(boolean b)           # start with 0 on Y-axis (useful for histograms)
>>> c1.setLegendFont(Font)            # set the legend font
>>> c1.setLegend(boolean b)       # draw the legent (0- do not draw it)
>>> c1.setLegendPosition(int axis, double b)  # axis=0 means x-axis, axis=1 means y-axis
>>> c1.setLogScale(int axis, boolean b)       # set log scale (b=1) or nor (b=0)
>>> c1.setTicsMirror(int axis, boolean b) # set ticks or not
>>> c1.setGrid(int axis, boolean b)       # show grid or not
>>> c1.setGridColor(Color c)                  # set grid color
>>> c1.setGridToFront(boolean b)              # set grid in front of drawing
>>> c1.setBox(boolean b)                      # set bounding box arround the graph
>>> c1.setBoxOffset(double f)                 # sets the offset of the bounding box
>>> c1.setBoxFillColor(Color c)               # sets the fill color of the bounding box
>>> c1.setBoxColor(Color c)                   # sets the color of the bounding box
>>> c1.setBackgroundColor(Color c)            # sets the background color of the graph.
>>> c1.setRange(int axis,double mi,double ma) # set range for axis (=0 or =1)
>>> c1.setRange(MinX,MaxX,MinY,MaxY)          # set range for X and Y (double arguments)
>>> c1.setAutoRange(int axis, boolean b)      # sets the auto-range for axis
>>> c1.setAutoRange(boolean b)                # sets the auto-range for X and Y
>>> c1.setLabel(String s, Font f, Color c)    # set random label

For Jython, use ``0'' for false and ``1'' for true. For JAVA and BeanShell, use the usual ``false'' and ``true''.

11.4 Saving the plot in an external file

You can export the plot into an image using the ``export'' method .i.e.

>>> c1.export("file.ps")

The plot will be exported to a PostScript file. You can also export it to png, jpeq, eps, pdf etc. Just use an appropriate extension for your output file. It is also useful to save the file using the same master name and the same directory where the script itself is located. In this case type this:

>>> c1.export(Editor.DocMasterName()+".ps")

where the method ``Editor.DocMasterName()'' access the file name of the currently opened file with the script.

It is also possible to save a Canvas in an image file with a pop-up dialog. You should use the method "c1.exportDialog(file)" for this.