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

This is an old revision of the document!

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([-1,2,3], [0.5, 0.2, 0.3])
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 documentation of HPlot in HPlot. 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 *
 3: c1 = HPlot("Canvas",500,400,2,2)
 4: c1.visible()
 5: c1. setRangeAll(0,10,0,10)
 6: h1 = P1D("Simple")
 9: h1.add(5,6);  c1.draw(h1)
12: h1.add(4,3); c1.draw(h1)
15: h1.add(3,3); c1.draw(h1)
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('')” 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 “”.

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 *
 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)
12: h1 = P1D("Simple1")
13: xpos=5
14: ypos=7
15: h1.add(xpos,ypos)
16: c1.draw(h1)
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:

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  *
 5: c1 = HPlotJa("Canvas",600,400,2,1)
 6: c1.visible(1)
 7: c1.showEditor(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)
19: c1.setAutoRange()
20: c1.setShowStatBox(0)
21: h2 = H1D("Histogram",15, -2.0, 2.0)
22: h2.setFill(1)
23: h2.setFillColor(
24: h2.setColor(
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)
# 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.

Showing real-time data

All canvas can be used to show data updated in real time. As example, we show how to display a random points, updating the data every 100 ms. Note that we use the method “clearData()”, which reloads data container but does not change its graphical attributes. A slower method is “clearAll()” which reloads graphical attributes of the canvas.

Code example

  Download for this example is disabled for non-members
 1: from java.util import Random
 2: from jhplot  import *
 4: c1 = HPlot("Test")
 5: c1.visible()
 6: c1.setRange(-2,2,-2,2)
 7: c1.setNameX('Time')
 8: c1.setNameY('Data')
 9: c1.setLegend(0)
11: r = Random()
12: import time
13: p=P1D("test")
14: for i in range(1000):
15:    c1.clearData()
16:    p.clear()
17:    x=r.nextGaussian()
18:    y=r.nextGaussian()
19:    p.add(x,y)
20:    c1.draw(p)
21:    time.sleep(0.1)

The method “clearData()” may produce some memory leak if labels for X or Y are should be drawn. It is advised to call “clearLabels()” method, in addition to “clearData()” to avoid a memory leak related to the font creation/destruction of labels. Read this this forum for details.

You can also use a lighter canvas, “SPlot”, which is much simpler and requires less resources.

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 *
 5: c1  = HPlot3D("Canvas",600,700,2,2)
 6: c1.visible(1)
 8: c1.setGTitle("HPlot3D canvas tests")
 9: r=Random()
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)
16: for i in range(1000):
17:     h1.fill(r.nextGaussian(),r.nextGaussian())
20: c1.draw(h1)
21: c1.setScaling(8)
22: c1.setRotationAngle(10)
23: c1.update()
26: c1.draw(f1)
27: c1.setScaling(8)
28: c1.setRotationAngle(30)
29: c1.update()
32: c1.draw(f2)
33: c1.setScaling(8)
34: c1.setRotationAngle(40)
35: c1.update()
38: c1.draw(f3)
39: c1.setAxesFontColor(
40: c1.setColorMode(4)
41: c1.setScaling(8)
42: c1.setElevationAngle(30)
43: c1.setRotationAngle(35)
44: c1.update()
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
 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)
16: p1=P2D("Galaxy")
17: p1.setSymbolSize(1)
18: p1.setSymbolColor(
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 canvases into external JFrame

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
 3: from jhplot import *
 4: from java.awt import *
 6: f1=F2D("x^2+sin(x)*y^2",-2,2,-2,2);
 7: c1=HPlot2D("Canvas",600,700)
 8: c1.visible()
11: c1.setName("2D function");
12: c1.setNameX("X variable")
13: c1.setNameY("Y variable")
15: c1.setStyle(2)
16: c1.draw(f1)
18: lab1=HLabel("ω test",0.7,0.55, "NDC")
19: lab1.setColor(Color.white)
20: c1.add(lab1,0.1)
22: lab2=HLabel("β test",0.5,-0.8, "USER")
23: c1.add(lab2,0.1)
25: # reduce font size for X
26: axis=c1.getAxis(0);
27: axis.setLabelHeightP(0.03)
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.1376624371.txt.gz · Last modified: 2013/08/15 21:39 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