You are a guest. Restricted access. Read more.
SCaVis manual

Plots in 2D

There are several canvases to show data in 2D: Plot, HPlot, HPlotJa, SPlot and many other pads.

Plots using MatLab syntax

Plot is a simplest canvas to show data and functions. It is less Java oriented, but more oriented towards MatLab syntax (which does not assume operation with objects and dynamical access of methods).

from jhplot  import *
 
j=Plot()
j.plot([-1,2,3], [0.5, 0.2, 0.3])
j.show()
j.export("a.pdf")   # save as PDF file
# j.export("a.eps") # save as EPS (PostScript) format
# j.export("a.svg") # save as SVG file

Here we created X-Y plot and saved as PDF image. You can also use the “savefig()” method, which is equivalent to the export method.

Interactive 2D canvaces

Below we discuss more complicated plot canvases which can be used to display data in 2D. Such canvaces usually have the names starting with the capital “H”. Typically, you can build a plot to show 2D data as this:

from jhplot *
c1 = HPlot("Canvas",600,400,2,1) # canvas size 600x400, 2 plot regions
c1.visible(100,200)  # make it visible and position at 100 , 200 pixels on the screen
c1.setAutoRange()    # autorange for X
c1.draw(object1)     # draw object1 (H1D,F1D,P1D etc)
c1.draw(object2)     # draw a new object
c1.export("figure.pdf") # export to PDF file

This code create a canvas with the size 600×400 (in pixels), it has 2 pads to show data. The method visible(100,200) make the canvas visible and sets its location on the screen at position 100 (in X from left conner) and 200 (from top) in pixels. If you want a default position, jut call “visible()”. Then you can draw any mathematical object or data. Then you can export the image to vector format. Now you are ready to plot functions, histograms and datasets. See the HPlot documentation. It should be noted the HPlot canvas can be replaced by any other canvas described above.

Several plotting regions

These canvases can be used to show several pads (plot regions) with plotted objects. Specify the number of divisions in X and Y in the constructors. The navigate to a specific plot region using the method “cd()”. Here is example of how to create 2×2 pads on a single canvas:

Code example

  Download for this example is disabled for non-members
 1: from jhplot  import *
 2:
 3: c1 = HPlot("Canvas",500,400,2,2)
 4: c1.visible()
 5: c1. setRangeAll(0,10,0,10)
 6: h1 = P1D("Simple")
 7:
 8: c1.cd(1,1);
 9: h1.add(5,6);  c1.draw(h1)
10:
11: c1.cd(1,2);
12: h1.add(4,3); c1.draw(h1)
13:
14: c1.cd(2,1);
15: h1.add(3,3); c1.draw(h1)
16:
17: c1.cd(2,2);
18: h1.add(2,1); c1.draw(h1)
19: c1.export ("example.pdf")

This works for HPlot, HPlotJa, HPlot3D and many other pads.

Here we use the same X and Y ranges. One can use “setAutoRange()” (after each “cd()”) method to set autorange for each pad. Also, try “setRange(xmin,xmax,ymin,ymax)” to set fixed ranges for each pads. it shows 4 pads with data points.

The plots are fully customizable, i.e. one can change any attribute (axis, label, ticks, ticks label). Read the section plot_styles about how to change various attributes and make different presentation styles.

Exporting to images

When functions, data_structures, histograms or diagrams are shown, one can create an image using the menu [File]-[Export]. One can also save image in a file using the method “export” to a variety of vector graphics formats as well as bitmap image formats.

In the example below, we save an instance of the HPlot class to a PDF file:

>>> c1.export('image.pdf')

we export drawings on the canvas HPlot (c1 represents its instance) into a PDF file. One can also export it into PNG, JPEG, EPS, PS etc. formats using the appropriate extension for the output file name.

As example, simply append the statement “c1.export('image.ps')” at the end of code snippets shown in sections functions, data_structures, histograms or diagrams, and your image will be saved to a PostScript file “image.ps”.

Images can be generated in a background (without a pop-up frame with the canvas). For this, use the method “c1.visible(0)”, where “0” means Java false.

Axis labels

Can be set using setNameX(“label”) and setNameY(“label”). These are global methods which should be applied to the HPlot canvas. However, every plotting object have their own methods, such as “setLabelX(“label”)” and setLabelY(“label”)“. If the labels are set to the object, the plot will display the object labels rather than those set using setNameX() and setNameY().

Ticks and subticks

One can redefine ticks using several methods of the HPlot

setRange(0,10,0,10)    # set range for X and Y 
setNumberOfTics(1)     # redefine ticks   
setNumberOfTics(0,2)   # set 2 ticks on X
setNumberOfTics(1,5)   # set 5 ticks on Y
setSubTicNumber(0,2)   # set 2 subticks on X
setSubTicNumber(1,4)   # set 4 subticks on Y

The simple example illustrates this:

Code example

  Download for this example is disabled for non-members
 1: from jhplot  import *
 2:
 3: c1 = HPlot("Canvas")
 4: c1.visible()
 5: c1.setRange(0,10,0,10)
 6: c1.setNumberOfTics(1)
 7: c1.setNumberOfTics(0,2)
 8: c1.setNumberOfTics(1,5)
 9: c1.setSubTicNumber(0,2)
10: c1.setSubTicNumber(1,4)
11:
12: h1 = P1D("Simple1")
13: xpos=5
14: ypos=7
15: h1.add(xpos,ypos)
16: c1.draw(h1)
17:
18: lab=HLabel("Point", xpos, ypos, "USER")
19: c1.add(lab)
20: c1.update()
21: c1.export ("example.pdf")

We labeled a point and generated a PDF files with the figure as shown in this figure:

 Ticks on axis

Showing shapes and objects

You can show data and functions together with different 2D objects. Here is a simple example that shows a histogram, a data set in X-Y and 3 ellipses:

 Shapes and data

Unregistered users have a limited access to this section. You can unlock advanced pages after becoming a full member. You can also request to edit this manual and insert comments.

Look at the Section Drawing shapes.

Post editing

HPlotJa can be used to edit figures. For example, one can make inserts if one creates 2 plotting pads and then one can edit the pads using the “mouse-click” fashion. For example, run this script:

Code example

  Download for this example is disabled for non-members
 1: from java.awt import Color
 2: from java.util import Random
 3: from jhplot  import  *
 4:
 5: c1 = HPlotJa("Canvas",600,400,2,1)
 6: c1.visible(1)
 7: c1.showEditor(1)
 8:
 9: c1.cd(1,1)
10: c1.setAutoRange()
11: c1.setShowStatBox(0)
12: p1 = P1D("data points")
13: rand = Random()
14: for i in range(500):
15:       p1.add(rand.nextGaussian(),rand.nextGaussian())
16: c1.draw(p1)
17:
18: c1.cd(2,1)
19: c1.setAutoRange()
20: c1.setShowStatBox(0)
21: h2 = H1D("Histogram",15, -2.0, 2.0)
22: h2.setFill(1)
23: h2.setFillColor(Color.blue)
24: h2.setColor(Color.blue)
25: for i in range(10000):
26:       h2.fill(1+rand.nextGaussian())
27: c1.draw(h2)

Then edit the figure (increase the size of one pad, and then drag the other one):

Similarly, one can achieve the same using the method “setPad()” where you can specify the location and the sizes of the plot regions The script below creates 2 plotting pads. The second pad is located inside the first one. Then you can plot data as usual, navigating to certain pads using the “cd(i,j)” method.

from jhplot  import  *
c1 = HPlotJa("Canvas",600,400,2,1)
c1.visible(1)
 
# change pad positions and sizes
c1.setPad(1,1,0.1, 0.1, 0.8,0.8)
c1.setPad(2,1,0.5, 0.14, 0.3,0.3)

Interactive plotting

HPlotJas canvas represents a way to prepare all objects for plotting, fitting and rebinning of data. Look at the Sect. Interactive Fit where this canvas is discussed in the context of plotting, rebinning and fitting.

Polar coordinates

For the polar coordinates, use the HChart canvas. A small code below shows ho to show a dataset filled from the X-Y array P1D

Unregistered users have a limited access to this section. You can unlock advanced pages after becoming a full member. You can also request to edit this manual and insert comments.

Example with 2D plot regions

In the example below, we create 4 plot regions (2 by 2) and plot functions and a histogram. Then we export the plot to EPS file for inclusion to a LaTeX document

Code example

  Download for this example is disabled for non-members
 1: from java.awt import Color,Font
 2: from java.util import Random
 3: from jhplot import *
 4:
 5: c1  = HPlot3D("Canvas",600,700,2,2)
 6: c1.visible(1)
 7:
 8: c1.setGTitle("HPlot3D canvas tests")
 9: r=Random()
10:
11: h1=H2D("My 2D Test1",30,-4.5, 4.5, 30, -4.0, 4.0)
12: f1=F2D("cos(x*y)*(x*x-y*y)", -2.0, 2.0, -2.0, 2.0)
13: f2=F2D("sin(4*x*y)", -2.0, 2.0, -2.0, 2.0)
14: f3=F2D("x^3-3*x-3*y^2", -2.0, 2.0, -2.0, 2.0)
15:
16: for i in range(1000):
17:     h1.fill(r.nextGaussian(),r.nextGaussian())
18:
19: c1.cd(1,1)
20: c1.draw(h1)
21: c1.setScaling(8)
22: c1.setRotationAngle(10)
23: c1.update()
24:
25: c1.cd(2,1)
26: c1.draw(f1)
27: c1.setScaling(8)
28: c1.setRotationAngle(30)
29: c1.update()
30:
31: c1.cd(1,2)
32: c1.draw(f2)
33: c1.setScaling(8)
34: c1.setRotationAngle(40)
35: c1.update()
36:
37: c1.cd(2,2)
38: c1.draw(f3)
39: c1.setAxesFontColor(Color.blue)
40: c1.setColorMode(4)
41: c1.setScaling(8)
42: c1.setElevationAngle(30)
43: c1.setRotationAngle(35)
44: c1.update()
45:
46: c1.export("figure.eps")  # export to EPS format

The output of this script is shown below:

Use setAutoRange() if you want X-Y-Z ranges taken from the function definitions. Otherwise, use “setRange()” to fix ranges.

Here is another example showing 2D plots with data:

Code example

  Download for this example is disabled for non-members
 1: from jhplot  import *
 2: from java.util import Random
 3: from java.awt import Color
 4:
 5: c1 = HPlot3D("Canvas",600,600)
 6: c1.setGTitle("Galaxy")
 7: c1.setNameX("X")
 8: c1.setNameY("Y")
 9: c1.visible(1)
10: c1.setElevationAngle(30)
11: c1.setBoxColor(Color.gray)
12: c1.setAxesFontColor(Color.gray)
13: c1.setBoxed(0)
14: c1.setRange(-10,10,-10,10,-10,10)
15:
16: p1=P2D("Galaxy")
17: p1.setSymbolSize(1)
18: p1.setSymbolColor(Color.blue)
19: rand = Random()
20: for i in range(10000):
21:       x=3*rand.nextGaussian()
22:       y=3*rand.nextGaussian()
23:       z=0.4*rand.nextGaussian()
24:       p1.add(x,y,z)
25: p2=P2D("Core")
26: p2.setSymbolSize(1)
27: p2.setSymbolColor(Color.yellow)
28: for i in range(5000):
29:       x=0.9*rand.nextGaussian()
30:       y=0.9*rand.nextGaussian()
31:       z=0.8*rand.nextGaussian()
32:       p2.add(x,y,z)
33: c1.draw(p1)
34: c1.draw(p2)

The output is shown below:

Embedding in JFrame

It is possible to embed SCaVis canvases in Java java.swing.JFrame, so you can build an application with custom buttons. Here is an example:

One can comment and discuss this section after becoming a full member.

Density plots

You can also make density plots in which color represent density (or values). Look at this rather comprehensive example which shows how to plot F2D functions or 2D histograms (H2D) using several pads:

Code example

  Download for this example is disabled for non-members
 1: # Canvas2D example. Showing 2D function and labels in HPlot3D
 2:
 3: from jhplot import *
 4: from java.awt import *
 5:
 6: f1=F2D("x^2+sin(x)*y^2",-2,2,-2,2);
 7: c1=HPlot2D("Canvas",600,700)
 8: c1.visible()
 9:
10:
11: c1.setName("2D function");
12: c1.setNameX("X variable")
13: c1.setNameY("Y variable")
14:
15: c1.setStyle(2)
16: c1.draw(f1)
17:
18: lab1=HLabel("ω test",0.7,0.55, "NDC")
19: lab1.setColor(Color.white)
20: c1.add(lab1,0.1)
21:
22: lab2=HLabel("β test",0.5,-0.8, "USER")
23: c1.add(lab2,0.1)
24:
25: # reduce font size for X
26: axis=c1.getAxis(0);
27: axis.setLabelHeightP(0.03)
28:
29: c1.update()

Other charts

SCaVis can be used to plot many other types of charts, such as Candlestick chart. Read Section about Financial charts.

Sergei Chekanov 2010/03/07 17:38

man/visual/plots2d.txt · Last modified: 2014/02/16 14:57 by admin
CC Attribution-Share Alike 3.0 Unported
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5