DMelt:Plots/9 Plot Styles by Example

From HandWiki
Member

Plot styles by example

This tutorial shows various styles of presenting data when using DataMelt. As usual, we make a small Jython code snippets to illustrate various Canvas styles.

The base DataMelt code which makes data for the examples below is the same. It looks as:

from java.util import Random
from jhplot import * 

c1 =HPlot("Canvas")
c1.visible()
c1.setRange(0,100,0,100)

h1 = H1D("Histogram",20, 50.0, 100.0)
f1=F1D("cos(x)*x",1,50)
p1= P1D("X-Y data")

rand = Random(10)
for i in range(500):
      h1.fill(85+10*rand.nextGaussian())
      if (i<200): p1.add(56+7*rand.nextGaussian(),70+7*rand.nextGaussian())
c1.draw(f1)
c1.draw(h1)
c1.draw(p1)

Below we show how to apply various graphic styles when presenting these 3 objects: a histogram, a function and data points.


Article styles

Here are "scientific" styles: plots are all in black and while, nothing fancy.

DMelt example: Style for 2D. Plain article style (BG color)

The code is shown below:

from java.awt import *
from java.util import Random
from jhplot import * 

def applyStyleGray(c1,f1,h1,p1):

     style="Article style (BG)"
     f1.setColor(Color.black)
     f1.setPenWidth(3)
     # apply style for histogram
     h1.setFill(1)
     h1.setPenWidth(2)
     h1.setErrX(0)
     h1.setErrY(1)
     h1.setFillColor( Color(0x787C80)  )


     # apply style for data points 
     p1.setSymbol(6)
     p1.setSymbolSize(6)
     p1.setColor( Color.black ) 
     return style 

# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,100)
c1.setMarginLeft(70)
c1.setNameX("X values")
c1.setNameY("Y values")

h1 = H1D("Histogram",20, 50.0, 100.0)
f1=F1D("cos(x)*x",1,50)
p1= P1D("X-Y data")

rand = Random(10)
for i in range(500):
      h1.fill(85+10*rand.nextGaussian())
      if (i<200): p1.add(56+7*rand.nextGaussian(),70+7*rand.nextGaussian())
        

# apply style 
style=applyStyleGray(c1,f1,h1,p1)
c1.setGTitle(style)


## now put a few points
c1.draw(f1)
c1.draw(h1)
c1.draw(p1)


# draw keys 
k=HKey(h1.getTitle(),h1)
k.setLocation(0.2, 0.8,"NDC")                     
c1.add(k)

k=HKey(p1.getTitle(),p1)
k.setLocation(0.2, 0.75,"NDC")
c1.add(k)

k=HKey(f1.getTitle(),f1)
k.setLocation(0.2, 0.7,"NDC")
c1.add(k)

# now show all objects
c1.update();

import sys
expfile=sys.argv[0].replace(".py",".png")
c1.export(expfile)


Presentation styles

Now we consider alternative styles that use a lot of colors:

DMelt example: Presentation style for 2D plots (colors)

The code is shown below:

from java.awt import *
from java.util import Random
from jhplot import * 

def applyStyleGray(c1,f1,h1,p1):

     style="Presentation style"
     st = Font.BOLD + Font.ITALIC;
     f = Font("Serif",st,14);

     c1.setAntiAlias(True) 
     c1.setGTitle(style,f,Color.red)    
     c1.setNameX("X values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setNameY("Y values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setTicFont(Font("Serif", Font.ITALIC, 14)) 
     c1.setAxisPenTicWidth(1) 
     c1.setGridAll(0,True)
     c1.setGridAll(1,True)

     f1.setColor(Color.blue)
     f1.setPenWidth(3)

     # apply style for histogram
     h1.setFill(1)
     h1.setPenWidth(2)
     h1.setErrX(0)
     h1.setErrY(1)
     h1.setFillColor( Color(0xFF0096)  )

     # apply style for data points 
     p1.setSymbol(4)
     p1.setSymbolSize(6)
     p1.setColor( Color.green ) 
     return style 


# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,100)
c1.setMarginLeft(70)

h1 = H1D("Histogram",20, 50.0, 100.0)
f1=F1D("cos(x)*x",1,50)
p1= P1D("X-Y data")

rand = Random(10)
for i in range(500):
      h1.fill(85+10*rand.nextGaussian())
      if (i<200): p1.add(56+7*rand.nextGaussian(),70+7*rand.nextGaussian())
        

# apply style 
style=applyStyleGray(c1,f1,h1,p1)


## now put a few points
c1.draw(f1)
c1.draw(h1)
c1.draw(p1)


# draw keys 
f=Font("Serif", Font.ITALIC, 18)
k=HKey(h1.getTitle(),0.2,0.8,f,Color.black,"NDC",h1 ) 
k.setKeySpace(-2) 
c1.add(k)

k=HKey(p1.getTitle(),0.2,0.75,f,Color.black,"NDC",p1 )                    
k.setKeySpace(2)
c1.add(k)

k=HKey(f1.getTitle(),0.2,0.7,f,Color.black,"NDC",f1 )
k.setKeySpace(-2)
c1.add(k)

# now show all objects
c1.update();

import sys
expfile=sys.argv[0].replace(".py",".png")
c1.export(expfile)

You can put LateX equations too:

DMelt example: Presentation style with logo and LaTeX equation

The code is shown below:

from java.awt import *
from java.util import Random
from jhplot import * 
from jhplot.shapes import * 

def applyStyleGray(c1,f1,h1,p1):

     style="Presentation style with logo and LaTeX equation"
     st = Font.BOLD + Font.ITALIC;
     f = Font("Serif",st,14);

     c1.setAntiAlias(True) 
     c1.setGTitle(style,f,Color.red)    
     c1.setNameX("X values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setNameY("Y values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setTicFont(Font("Serif", Font.ITALIC, 14)) 
     c1.setAxisPenTicWidth(1) 
     c1.setGridAll(0,True)
     c1.setGridAll(1,True)

     f1.setColor(Color.blue)
     f1.setPenWidth(3)

     # apply style for histogram
     h1.setFill(1)
     h1.setPenWidth(2)
     h1.setErrX(0)
     h1.setErrY(1)
     h1.setFillColor( Color(0xFF0096)  )


     # apply style for data points 
     p1.setSymbol(4)
     p1.setSymbolSize(6)
     p1.setColor( Color.green ) 


     return style 


# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,100)
c1.setMarginLeft(70)

h1 = H1D("Histogram",20, 50.0, 100.0)
f1=F1D("cos(x)*x",1,50)
p1= P1D("X-Y data")

rand = Random(10)
for i in range(500):
      h1.fill(85+10*rand.nextGaussian())
      if (i<200): p1.add(56+7*rand.nextGaussian(),70+7*rand.nextGaussian())
        

# apply style 
style=applyStyleGray(c1,f1,h1,p1)


## now put a few points
c1.draw(f1)
c1.draw(h1)
c1.draw(p1)


# draw keys 
f=Font("Serif", Font.ITALIC, 18)
k=HKey(h1.getTitle(),0.2,0.8,f,Color.black,"NDC",h1 ) 
k.setKeySpace(-2) 
c1.add(k)

k=HKey(p1.getTitle(),0.2,0.75,f,Color.black,"NDC",p1 )                    
k.setKeySpace(2)
c1.add(k)

k=HKey(f1.getTitle(),0.2,0.7,f,Color.black,"NDC",f1 )
k.setKeySpace(-2)
c1.add(k)

# put here a picture
#pic=  Picture(0.81, 0.95, "globus12_small.png")
#pic.setPosCoord("NDC")
#c1.add(pic)

# show latex equation
lab=HLabelEq("\int_{i=1}^{100}\omega_{i}(x)dx", 5, 50, "USER")
lab.setFontSize(18)
lab.setColor(Color.blue);
c1.add(lab)


# now show all objects
c1.update();

import sys
expfile=sys.argv[0].replace(".py",".png")
c1.export(expfile)

Or you can change background:

DMelt example: Presentation style with many colors and insert image

The code is shown below:

from java.awt import *
from java.util import Random
from jhplot import * 
from jhplot.shapes import * 


def applyStyleGray(c1,f1,h1,p1):

     style="Presentation style with many colors"
     st = Font.BOLD + Font.ITALIC;
     f = Font("Serif",st,22);

     c1.setAntiAlias(True) 
     c1.setGTitle(style,f,  Color(0x4585E6) )    
     c1.setNameX("X values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setNameY("Y values",Font("Serif", Font.ITALIC, 20),Color.blue)
     c1.setTicFont(Font("Serif", Font.ITALIC, 14)) 
     c1.setAxisPenTicWidth(1) 
     c1.setGridAll(0,True)
     c1.setGridAll(1,True)

     c1.setBackgColorForAllGraph( Color(0xC9CBCC) ) 
     c1.setBox(True) 
     cc= Color(0x4585E6)
     c1.setTicColor(0,cc) 
     c1.setTicColor(1,cc)     
     c1.setShadow(True) 
     c1.setAxesColor(cc) 


     f1.setColor(Color(0xF2FFBF))
     f1.setPenWidth(5)

     # apply style for histogram
     h1.setFill(1)
     h1.setPenWidth(2)
     h1.setErrX(0)
     h1.setErrY(1)
     h1.setFillColor( Color(0xE6ACE6)  )
     h1.setColor(Color(0xF2FFBF))


     # apply style for data points 
     p1.setSymbol(4)
     p1.setSymbolSize(6)
     p1.setColor( Color.green ) 
     return style 


# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,100)
c1.setMarginLeft(70)

h1 = H1D("Histogram",20, 50.0, 100.0)
f1=F1D("cos(x)*x",1,50)
p1= P1D("X-Y data")

rand = Random(10)
for i in range(500):
      h1.fill(85+10*rand.nextGaussian())
      if (i<200): p1.add(56+7*rand.nextGaussian(),70+7*rand.nextGaussian())
        

# apply style 
style=applyStyleGray(c1,f1,h1,p1)


## now put a few points
c1.draw(f1)
c1.draw(h1)
c1.draw(p1)


# draw keys 
f=Font("Serif", Font.ITALIC, 18)
k=HKey(h1.getTitle(),0.2,0.8,f,Color.white,"NDC",h1 ) 
k.setKeySpace(-2) 
c1.add(k)

k=HKey(p1.getTitle(),0.2,0.75,f,Color.white,"NDC",p1 )                    
k.setKeySpace(2)
c1.add(k)

k=HKey(f1.getTitle(),0.2,0.7,f,Color.white,"NDC",f1 )
k.setKeySpace(-2)
c1.add(k)

# put a picture here
#pic=  Picture(0.81, 0.95, "globus12_small.png")
#pic.setPosCoord("NDC")
#c1.add(pic)



# now show all objects
c1.update();

import sys
expfile=sys.argv[0].replace(".py",".png")
c1.export(expfile)