DMeltTutorial:3 Data Containers

From jWork.ORG
Revision as of 19:53, 27 December 2016 by Jworkorg (talk | contribs) (N-dimensional arrays)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
Contents


1D arrays

1D arrays in DataMelt are designed for numerical calculations and are optimized for numbers. We first consider P0D arrays (to hold double values) and P0I (to hold integer values)


from jhplot import P0D
p0=P0D("test")
for i in range(10):
      p0.add(i)
print p0.toString()

One can view the data containers using several methods. One is toString() which converts data into a string. One can write data into a file (including a compression) using the method toFile(file). One can also view data in a sortable table using the method toTable():


from jhplot  import P0D
p0=P0D("test")
for i in range(1,50,2):
      p0.add(i)
p0.toTable()

As for any DataMelt data object, one can write and read arrays into files using the method "toFile" and read using the method "read()"

p0.toFile("data.txt")

and read it back as:

p0.read("data.txt")

If a file was zipped use the method "readZip()".

One can access various statistical characteristics of the P0D arrays as:


from jhplot  import P0D
p0=P0D("test")
for i in range(1,50,2):
      p0.add(i)
print p0.getStatString()

<fc #008000>Exercise: try to increase the number of iteration to 1000 and see the output. </jnote>


2D arrays

2D arrays are based on the Java class P1D. This class can be used to show data in X-Y plane As before, one can fill such arrays using the method "add(x,y)".

Here is a simple example how to show several data sets on on X-Y graph:


from jhplot  import  P1D, HPlot
from java.awt import Color

p1=P1D("1st data set")
p1.add(1,2); p1.add(2,3); p1.add(4,5)

p2=P1D("2nd data set")
p2.add(-1,3); p2.add(5,-2); p2.add(1,0)
p2.setColor(Color.red)

c1 = HPlot("Canvas")
c1.visible()
c1.setAutoRange()
c1.draw(p1); c1.draw(p2)

N-dimensional arrays