DMelt:Plots/8 Plot Annotations

From HandWiki

Plot annotations

Data can be annotated using interactive legends and labels. In this section we will briefly discuss main components to annotate data points. For annotations, use strings. You can include Greek or math symbols as explained below.

Greek symbols

All labels and legends or any string which can be shown on a canvas can contain Greek letters. The table below shows how to encode Greek letters:

<html>

Entity nameAppearanceEntity nameAppearanceEntity nameAppearance
&alpha;α&beta; β &gamma; γ
&delta; δ &epsi; ε &zeta; ζ
&eta; η &theta; θ &thetav; ϑ
&iota; ι &kappa; κ &lambda; λ
&mu; μ &nu; ν &xi; ξ
&pi; π &piv; ϖ &rho; ρ
&sigma; σ &sigmav; ς &tau; τ
&upsi; υ &phi; φ &phiv; ϕ
&chi; χ &psi; ψ &omega; ω
&Gamma; Γ &Delta; Δ &Theta; Θ
&Lambda; Λ &Xi; Ξ &Pi; Π
&Sigma; Σ &Upsi; ϒ &Phi; Φ
&Psi; Ψ &Omega; Ω

</html>

Mathematical operators

All labels and legends or any string which can be shown on a canvas can contain math operators:

<html>

Entity nameAppearanceEntity nameAppearanceEntity nameAppearance
&perp;&plusmn; ± &or;
&and;&le;&ge;
&equiv;&ap;&ne;
&sub;&sube;&sup;
&supe;&isin;&larr;
&rarr;&uarr;&darr;
&harr;&lArr;&rArr;
&uArr;&dArr;&iff;
&forall;&exist;&inf; &inf;
&nabla;

</html>

You can find more symbols in Unicode Consortium for HTML symbols.

Alternatively, you can use characters using numbers. See the unicode table unicode table When defining a Unicode character, use "&", followed by "#", then a number and then close it with a semi-column. For example, the string:

s="W^{ &#8723;}"

will show a W boson with minus and plus sign, <html>∓</html>.

Simple labels

The package jhplot.shapes can be used to display several geometrical primitives, pictures, including the text labels. All of this works together with the jhplot.HPlot jhplot.HPlot canvas. You cannot interact with the mouse using such labels.

Interactive labels

You can annotate your figure on the jhplot.HPlot jhplot.HPlot canvas putting interactive labels, so you can move them around with the mouse and edit.

The text labels can be set manually or via the label property window (click on the interactive label to bring up a dialog). Subscripts and superscripts in the text should be included as for the standard LaTeX syntax.

  • "X^{2}" will be shown as [math]\displaystyle{ X^{2} }[/math]
  • "X_{2}" will be displayed as [math]\displaystyle{ X_{2} }[/math]


To indicate over-line, use the reserved word #bar{}. For example, to display [math]\displaystyle{ \overline{X+Y} }[/math], use #bar{X+Y}. To display a square root $\sqrt{X+Y}$, use #sqrt{X+Y}.

To make a space between characters, use the usual space. To make a back-space, use the predefined keyword #bs2{}. This creates a backspace with the width of one character. To make a smaller backspace, equal to 1/2 of the character width, use #bs1{}.

One can mix this syntax to show complicated mathematical equations in the labels.

Symbols must be encoded using the entity reference notation as shown in the above sections. For example, the Greek "omega" should be written as "ω"

This is a small example that shows data points and create labels using mathematical symbols and Greek letters.

The Jython / Python script that creates this figure is given below. You can use the Java syntax to create a similar plot.

from jhplot import *
from java.awt import *

c1=HPlot("plot")
c1.setMarginLeft(80)
c1.setMarginBottom(50)
c1.setRange(1,220, 0, 7)
c1.setNameX("L [ab^{-1}]")
c1.setNameY("M_{φ} [GeV^{-1}]")
c1.setLogScale(0,1)
c1.visible()

p1=P1D('#sqrt{s}#bs2{}=100 TeV')
p1.setStyle("l")
p1.setPenWidth(4)
p1.add(1,1.2)
p1.add(3,1.8)
p1.add(10,2.0)
p1.add(30,3.1)
p1.add(100,4.0)
p1.add(200,5.0)

p2=p1.copy()
p2.setColor(Color.red)
p2.setStyle("p")

lab5=HLabel("#sqrt{β+j}≥∑α_{i}",0.5,0.84, "NDC")
lab5.setFont(Font("Serif", Font.ITALIC, 24))
lab5.setColor(Color.blue)
c1.add(lab5)
lab6=HLabel("#bar{q}#bs2{} q → δ →	μ",0.50,0.74, "NDC")
lab6.setFont(Font("Arial", Font.BOLD, 24))
lab6.setColor(Color.red)
c1.add(lab6)

c1.draw(p1)
c1.draw(p2)
#c1.export("plot.eps")
#c1.export("plot.pdf")

which shows this image:

DMelt example: An example of a plot with mathematical symbols and greek letters


Interactive legends

Interactive legends are used to annotate data points, functions or histograms. Be default, once data are plotted, data annotation is also displayed using title of the data (getTitle()) as a text for the key. Still, one can set legends for data manually. Look at the API of the jhplot.HKey jhplot.HKey class.

In this example, we have disabled automatic legends and made legends using the jhplot.HKey jhplot.HKey class. We plot 3 different objects, which we pass to the HKey to make legends.

from java.awt import Font,Color
from jhplot  import  HPlot,HKey

c1 = HPlot("Canvas",600,500)
c1.visible()
c1.setGridAll(0,0)
c1.setGridAll(1,0)
c1.setGTitle("HKey types")
c1.removeAxes()
c1.setRange(0,100,0,100)

for j in range(0,13):
     title = "key type="+str(j)
     hh = HKey(title,15,97-7*(j+1))
     c= Color(30+j*10,65 + j*10,10*j)
     hh.setKey(j,2.0,c)
     hh.setKeySpace(4.0)
     c1.add(hh)

h = HKey("key type=20",55,90)
h.setKey(20,7.0,Color.blue)
h.setKeySpace(4.0)
c1.add(h)


h1 =HKey("key type=21",55,83)
h1.setKey(21,7.0,Color.blue)
h1.setKeySpace(4.0)
c1.add(h1)
    
h1 =HKey("key type=30",55,76)
h1.setKey(30,7.0,Color.blue)
h1.setKeySpace(4.0)
c1.add(h1)
    

h1 =HKey("key type=31",55,69)
h1.setKey(31,7.0,Color.green)
h1.setKeySpace(4.0)
c1.add(h1)

h1 =HKey("key type=32",55,62)
h1.setKey(32,7.0,Color.red)
h1.setKeySpace(4.0)
c1.add(h1)







c1.update()

The position of legends are set automatically. This example generates the figure below:

DMelt example: HKey key shown on HPlot

The legend can show symbols, lines or filled areas, and in fact, their positions can be set manually. In fact, to make a legend, there is no need to use input plotted objects.

Here we created an interactive label (type=0) at the position x=15,y=100 which shows a symbol (open circle). The type of the symbol (0) is set using the method "setKey".

Legends are fully customizable. In the example below we change many attributes of legends

DMelt example: Multiple objects and manually setting interactive keys with HKey

The figure was generated with the example code shown below. Here we change many default attributes of the standard legends using the Python codding:

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

# make empty canvas in some range
c1 =HPlot("Canvas")
c1.setGTitle("Testing data annotaions (HKey)")
c1.visible()
c1.setLegend(0)
c1.setAutoRange()


h1 = H1D("hist1",20, -2.0, 2.0)
h2 = H1D("hist2",10, -2.0, 2.0)
f1=F1D("50*x*x+100",-2,3)

p1= P1D("data1")
p1.setSymbol(5)
p1.setSymbolSize(10)
p1.setColor(Color.blue)

p2= P1D("data2")
p2.setColor(Color.red)

rand = Random()
for i in range(5000):
      h1.fill(rand.nextGaussian())
      h2.fill(1.0+rand.nextGaussian())
      if (i<20): p1.add(-2+0.4*rand.nextGaussian(),300+30*rand.nextGaussian())
      if (i<40): p2.add(-1.6+0.1*rand.nextGaussian(),500+30*rand.nextGaussian())
        
c1.draw(f1)
h1.setColor(Color(15,167,83))
h1.setPenWidth(2)
c1.draw(h1)


h2.setFill(1)
h2.setPenWidth(2)
h2.setFillColor(Color(122,20,157))
h2.setErrX(0)
h2.setErrY(1)
c1.draw(h2)

k=HKey("histo1",h1)
c1.add(k)

k=HKey("histo2",h2)
c1.add(k)


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



k=HKey("data1",p1)
c1.add(k)

k=HKey("data2",p2)
c1.add(k)

k=HKey("10*x*x",f1)
c1.add(k)

# now show all objects
c1.update();

LaTeX equations

If you are using jhplot.HPlot jhplot.HPlot, you can show complex LaTeX equations using the class jhplot.HLabelEq jhplot.HLabelEq. This example shows how to make such a label using NDC coordinate system. It also shows to to put an interactive label

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

c1 = HPlot("Canvas",600,400,1, 1)
c1.visible(1)
c1.setAutoRange()
c1.setMarginLeft(70)

h1 = H1D("Simple1",30, -2, 2.0)
rand = Random()
for i in range(1000):
      h1.fill(rand.nextGaussian())

c1.draw(h1)
c1.setAutoRange()
h1.setPenWidthErr(2)
c1.setNameX("X_{sub}")
c1.setNameY("Y^{sup}")
c1.setName("Canvas title for F_{2}")

# set HLabel in the normilised coordinate system
lab=HLabel("HLabel F_{2}", 0.8, 0.7, "NDC")
lab.setColor(Color.blue)
c1.add(lab)

# show latex equation 
lab=HLabelEq("\int_{i=1}^{100}\omega_{i}(x)dx+\sum_{i=0}^{200}\sqrt{i^2}", 0.15, 0.8, "NDC")
lab.setFontSize(16)
lab.setColor(Color.blue);
c1.add(lab)

c1.update()

The output is shown here:

DMelt example: Annotations on 2D canvaces. Show a LaTeX equation