Subsections


3.16 Plotting data in 3D. HPlot3D class

3.16.1 Plotting data points in 3D. P2D class

To draw data and histograms in 3D, the user should use the HPlot3D canvas. To plot data in three dimensions, one should use the P2D class which holds data to be shown in 3D. It is rather similar to the P1D, the only difference is that it keeps data in 3D phase space (X,Y,Z). Also, it has less options for drawing and errors for each component are not supported. Below is an example of to how draw points in 3D:

>>> from jhplot  import HPlot3D,P2D 
>>> from java.awt import Color
>>>
>>> c1 = HPlot3D("Canvas",600,400)
>>> c1.visible(1)
>>> c1.setNameX("X axis")
>>> c1.setNameY("Y axis")
>>> c1.setRange(-5,10,-5,5,-10,20) # set range
>>>
>>> h1= P2D("3D data set")
>>> h1.setSymbolSize(6)
>>> h1.setSymbolColor(Color.blue)
>>> h1.add(1.,2.,3.,) # add  X,Y,Z
>>> h1.add(4.,4.,5.,) # add  X,Y,Z
>>> c1.draw(h1)       # draw in 3D
>>>
>>> h1= P2D("new 3D data set")
>>> h1.setSymbolSize(6)
>>> h1.setSymbolColor(Color.red)
>>> h1.add(-1.,2.,1.,) # add  X,Y,Z
>>> h1.add(5.,0.,4.,) # add  X,Y,Z
>>> c1.draw(h1)       # draw again in red
There is no need to specify the setRange() method, since the range is set automatically. Look at the example p2d_points.py.

Finally, it should be noted that the HPlot3D canvas does not use Java3D at all!

3.16.2 Methods of the HPlot3D

The HPlot3D is rather similar to the HPlot: One can display several plots on the same canvas and change the plotted regions using the cd() method:

>>> from jhplot import HPlot3D
>>>
>>> c1 = HPlot3D("Canvas",600,400,2,2) # create 4 regions (2x2) 
>>> c1.visible(1)
>>> c1.cd(1,1)                         # go to the first region and plot something

Here are several important methods associated with the HPlot3D:

>>> c1.setRotationAngle(10)  # set rotation angle to 10 deg 
>>> c1.setScaling(8)         # set scaling to 8 (default is 12)
>>> c1.setElevationAngle(30) # set elevation angle to 30 deg
>>> c1.setAxesFontColor(Color.blue) # set fonts for axes labels
>>> c1.setColorMode(1)       # set color mod (from 1 -4)

Look at the example hplot3D.py for more details.

3.16.3 SHPlot3D class

Similar to SHPlot, one can create a singleton representing the canvas object using the static class SHPlot3D. In this case, every execution of the scripts in the jHepWork IDE does not create a new object of the canvas, but it just redraws it. This is an example of how to create such canvas:

>>> from jhplot  import H2D,SHPlot3D
>>> from java.util import Random
>>> c1=SHPlot3D.getCanvas()
>>> c1.setGTitle("Global title");
>>> c1.setNameX("Xaxis")
>>> c1.setNameY("Yaxis")
>>> c1.visible(1)
>>> ....

All methods of HPlot3D are also applicable to the SHPlot3D class.

3.16.4 Plotting extended objects in 3D. P3D class

To plot extended objects in three dimensions, like lines and cubes, use the P3D container. The construction of this container is similar to P2D. The only difference is that each data point in X,Y,Z, has an additional parameter representing an extension of the point in the corresponding direction.

>>> from jhplot  import HPlot3D,P3D 
>>> from java.awt import Color
>>>
>>> c1 = HPlot3D("Canvas",600,400)
>>> c1.visible(1)
>>> c1.setNameX("X axis")
>>> c1.setNameY("Y axis")
>>> c1.setRange(-5,10,-5,5,-10,20) # set range
>>>
>>> h1= P3D("3D object")
>>> h1.setPenColor(Color.blue)
>>> h1.add(4.0,1.0,8.0,2.0,3.0,1.0)   # build a cube, with X,dX,Y,dY,Z,dZ   
>>> h1.add(5.0,2.0,3.0,1.0,8.0, 0.0)  # make a line long Z
>>> c1.draw(h1)       # draw in 3D
>>>
>>> h2= P3D("new  3D objects")
>>> h2.setPenColor(Color.red)
>>> h2.add(-0.5,3.0,-1.0,2.0,6.0,2.0)  # build a cube, with X,dX,Y,dY,Z,dZ 
>>> c1.draw(h2)       # draw again in red
Look at the example p3d_points.py.

3.16.5 Plotting 2D functions in interactive 3D frame

As before, to plot 2D functions or histograms, one should use again the HPlot3D canvas. Here is a typical example which shows how to plot two functions on the same plot:

>>> from jhplot  import F2D,HPlot3D
>>>
>>> c1 = HPlot3D("Canvas",600,400)
>>> c1.visible(1)
>>> c1.setNameX("X axis")
>>> c1.setNameY("Y axis")
>>> f1 = F2D("2*exp(-x*y/20)+10*sin(pi*x)/y", -2.0, 5.0, -2.0, 5.0)
>>> f2 = F2D("4*x*y", -2.0, 5.0, -2.0, 5.0)
>>> c1.draw(f1,f2)
Here, the F2D class is the main class to build a 2D function.

3.16.6 Plotting 2D histograms in 3D

The H2D class was derived from the class Histogram2D of JAIDA. One can plot 2D histograms in the same way as 2D functions using the HPlot3D canvas:

>>> from jhplot  import H2D,HPlot3D
>>> from java.util import Random
>>>
>>> c1 = HPlot3D("Canvas",600,400)
>>> c1.visible(1)
>>> c1.setNameX("X axis")
>>> c1.setNameY("Y axis")
>>> h1 = H2D("My 2D Test 1",30,-3.0, 3.0, 30, -3.0, 3.0)
>>> rand = Random()
>>> for i in range(5000):
...          h1.fill(rand.nextGaussian(),rand.nextGaussian())
>>> c1.draw(h1)
By default, histograms are shown with bars. The color style can be changed using setColorMode(int) method (0 - WIREFRAME, 1 - HIDDEN, 2 - SPECTRUM IN COLOR, 3 - GRAYSCALE, 4 - DUALSHADES).

The histograms can also be shown as surface. In this case, more options are possible for drawing using the Tool menu of the the HPlot3D frame. To show a histogram as a surface, use setSurface(). In this case, the number of bins in X and Y should be set the same.

One can overlay two 2D histograms on one figure, if they are plotted using the surface option. In addition, you can plot histogram and a function on the same HPlot3 plot. See more complete examples in histo2D_2h.py and histo2D_2hf.py.