Subsections

3.4 Plotting functions in 1D

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

>>> from jhplot import  HPlot,F1D 
>>>
>>> c1=HPlot("Canvas")
>>> c1.visible()
>>> 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 specify the range for the abscissa. One can find all the methods associated with this class as explained in Section :autorefIntroduction to jHepWork1.4.3. It should be noted that the draw() method can also be used to draw arrays of F1D functions.

After importing the Color class from the Java AWT package, one can draw a function using various colors. Also, one can define the width for the line:

>>> from java.awt import Color
>>> from jhplot import F1D
>>>
>>> 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 the setPenDash() method. One can change the dashed line length by specifying an integer value between 0 and 40. One can also use the update() method to redraw the plot.

3.4.1 How to use JAIDA library

This is rather simple. Assume that a IFunction object was created using JAIDA. Then simple do:

>>> from jhplot import F1D
>>>
>>> f1=F1D(m_IFunction, -2.0, 5.0)
>>> c1.draw(f1)
where m_IFunction is a variable of the type IFunction. See the example histo_fit.py.

3.4.2 Plotting 1D functions on the same plot

In order to plot several functions on the same canvas, simply use:

>>> 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)

3.4.3 Plotting 1D functions in different plot regions

To plot two or more functions in different plot regions, one should use an appropriate canvas. The plotting region can be changed using the cd() method before starting to draw a function:

>>> from java.awt import Color
>>> from jhplot  import F1D,HPlot
>>>
>>> c1=HPlot("Canvas",600,400,1,2)
>>> c1.visible() 
>>> 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) 
>>> c1.draw(f1)
>>> c1.cd(1,2)         # go to the second plot
>>> c1.draw(f2)
One can learn about the H1D methods by typing c1. and pressing F4 key using the jHepWork Code Assist. 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.visible()              # as before 
>>> c1.setAutoRange()         # set autorange
>>> c1.cd(int, int)           # go to a specific region for plotting
>>> c1.update()               # update a plot 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)    # do not show the global title
>>> c1.setGTitle(String,Font,Color) # set the global title with Font and Color
>>> c1.viewHisto(boolean)      # start with 0 on Y-axis (for histograms)
>>> c1.setLegendFont(Font)     # set the legend font
>>> c1.setLegend(boolean)      # draw the legend (0- do not draw it)
>>> c1.setLegendPosition(int, double)     # 0 means x-axis; 1 means y-axis
>>> c1.setLogScale(int axis, boolean)     # set log scale (1) or not (0)
>>> c1.setTicsMirror(int axis, boolean)   # ticks?
>>> c1.setGrid(int axis, boolean)         # show grid or not
>>> c1.setGridColor(Color)                # grid color
>>> c1.setGridToFront(boolean)            # grid in front of drawing
>>> c1.setBox(boolean)                    # bounding box around the graph
>>> c1.setBoxOffset(double)               # offset of the bounding box
>>> c1.setBoxFillColor(Color)             # fill color of the bounding box
>>> c1.setBoxColor(Color)                 # color of the bounding box
>>> c1.setBackgroundColor(Colo)           # background color of the graph.
>>> c1.setRange(int,double,double)        # range for axis (=0 or =1)
>>> c1.setRange(MinX,MaxX,MinY,MaxY)      # range for X and Y (double arguments)
>>> c1.setAutoRange(int, boolean)         # auto-range for axis=0,1
>>> c1.setAutoRange(boolean b)            # auto-range for X and Y
>>> c1.setLabel(String, Font, Color)      # set random label

3.4.4 Saving a plot in an external file

One can export the plot into an image using the following export method:

>>> c1.export("file.ps")  # export plot to a PS file
The plot will be exported to a PostScript file. You can also export it to PNG, JPEG, EPS, PDF etc. formats by using the appropriate extension for the 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:

>>> c1.export(Editor.DocMasterName()+".ps")
where the method Editor.DocMasterName() accesses the file name of the currently opened script.

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

3.4.5 View an image. IView class.

If an image was saved in JPG, GIF or PNG formats, one can view it using the IView class. This is a typical example when an image is first saved in a file and then the IView is used to view the created image:

>>> file=Editor.DocMasterName()+".png"  # file name based on the opened script 
>>> c1.export(file)         # export to a PNG file 
>>> IView(file)             # view the created image

3.4.6 Analyze or edit an image. IEditor class.

If an image was saved in JPG, GIF or PNG formats, one can edit it (crop, resize, add a text, etc ) using the IEditor class. This class calls an ImageJ, an advanced java image editor [].

This is a typical example when a canvas (HPlot, Hplot3D, etc) with plots is first saved in a file and then the IEditor is used to edit and analyse the created image:

>>> file=Editor.DocMasterName()+".png"  # file name based on the opened script
>>> c1.export(file)         # export to a PNG file
>>> IEditor(file)           # edit the created image