Subsections


2.12 Plotting data using interactive 3D canvas. HPlot3D class

2.12.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!

2.12.2 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.

2.12.3 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.

2.12.4 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.