 |
Shows X-Y data points in 2D using different colors
Source code name: "hvisadd_2D_dots.py"
Programming language: Python
Topic: Data arrays/P2D
DMelt Version 1.9. Last modified: 07/10/1971. License: Pro
https://datamelt.org/code/cache/hvisadd_2D_dots_316.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
from java.awt import *
from visad import Display,RealTupleType,ScalarMap,RealType
from visad import FunctionType,FlatField,Irregular2DSet,DataReferenceImpl
from jhplot import *
from java.util import Random
c1=HVisAd("2D example", False)
display=c1.getDisplay()
XX = RealType("X")
YY = RealType("Y")
weight = RealType("weight")
domain_tuple = RealTupleType(XX,YY)
# fill random numbers
r=Random()
X,Y=[],[]
sss=[]
for j in range(10000):
if (j<2000):
X.append(r.nextGaussian())
Y.append(r.nextGaussian())
sss.append(1)
else: # data shifted and with different color (weight)
X.append(r.nextGaussian()+2)
Y.append(r.nextGaussian()+2)
sss.append(4+3*r.nextGaussian()) # rest with different random numbers
ntuple=[X,Y]
domain_set = Irregular2DSet(domain_tuple, ntuple)
print domain_set
# this part add colors to data points
values=[]
values.append([sss][0])
ftype = FunctionType(domain_tuple, weight)
field = FlatField(ftype, domain_set)
field.setSamples(values, False)
# put some colors
dR=display.getDisplayRenderer()
dR.setForegroundColor(Color.gray)
dR.setBackgroundColor(Color.black)
dR.setCursorColor(Color.red)
# X, Y are the usual axes
mX=ScalarMap( XX, Display.XAxis )
mY=ScalarMap( YY, Display.YAxis )
mX.setScalarName("X values")
mY.setScalarName("Y values")
mX.setRange(-5.0, 5.0) # set ranges
mY.setRange(-10.0, 10.0)
mX.getAxisScale().setSnapToBox(True)
mY.getAxisScale().setSnapToBox(True)
# add axis
display.addMap( mX )
display.addMap( mY )
# color for points
color = ScalarMap(weight, Display.RGBA)
display.addMap(color)
# set points
dispGMC = display.getGraphicsModeControl()
dispGMC.setPointMode(True);
dispGMC.setPointSize(3.0)
# Create a data reference and set the FlatField as our data
data_ref = DataReferenceImpl("data_ref")
data_ref.setData(field)
display.addReference( data_ref )
c1.visible()
You see the box below because you did not login.