X
63

How to plot two graphics with different data extents in the same view using IDL Object Graphics

With IDL Object Graphics, plotting two graphics with different data extents in the same graphic window requires normalizing the range of the second graphic to fit the extension of the first one. If not, the two graphics will not fit within the same view.

Here is an example of such normalization based on two random datasets, with two different extents along the Y-axis.

 

Code example:

 

PRO test_IDLgrPlot


;create 2 random datasets

d1 = SMOOTH(RANDOMU(Seed1, 100)*(-200),10)

d2 = SMOOTH(RANDOMU(Seed, 100)*0.1,5)


;create window hierarchy

mywindow = IDLgrWindow()

myview = IDLgrView()

mymodel = IDLgrModel()


; create plot objects

plot1= IDLgrPlot(INDGEN(100),d1,COLOR=[0,0,255])

plot2= IDLgrPlot(INDGEN(100),d2,COLOR=[0,255,0])


; add plot objects and model to the view

myview.Add, mymodel

mymodel.Add, plot1

mymodel.Add, plot2


; normalized plot1 extension (blue line) to fit the view

plot1.GetProperty, XRANGE = xr, YRANGE = yr

xs = NORM_COORD(xr)

xs[0] = xs[0] - 0.5

ys = NORM_COORD(yr)

ys[0] = ys[0] - 0.5

plot1.SetProperty, XCOORD_CONV = xs, YCOORD_CONV = ys


; normalized plot2 extension (green line) to fit the view and to fit the same extension as plot1

plot2.GetProperty, XRANGE = xr2, YRANGE = yr2

xs2 = NORM_COORD(xr2)

xs2[0] = xs2[0] - 0.5

ys2 = NORM_COORD(yr2)

ys2[0] = ys2[0] - 0.5

plot2.SetProperty, XCOORD_CONV = xs2, YCOORD_CONV = ys2


; create axis

oTextXAxis = IDLgrText('X axis')

oTextYAxis = IDLgrText('Blue line')

oTextYAxis2 = IDLgrText('Green line')


oPlotYAxis = IDLgrAxis( 1, /EXACT, RANGE = yr, $

    XCOORD_CONV = xs, YCOORD_CONV = ys,TITLE = oTextYAxis, $

    LOCATION = [xr[0], yr[0]], TICKDIR = 0, $

    TICKLEN = (0.02*(xr[1] - xr[0])))


oPlotXAxis = IDLgrAxis( 0, /EXACT, RANGE = xr, $

    XCOORD_CONV = xs, YCOORD_CONV = ys,TITLE = oTextXAxis, $

    LOCATION = [xr[0], yr[0]], TICKDIR = 0, $

    TICKLEN = (0.02*(yr[1] - yr[0])))


oPlotYAxis2 = IDLgrAxis( 1, /EXACT, RANGE = yr2, $

    XCOORD_CONV = xs, YCOORD_CONV = ys2, TITLE = oTextYAxis2,$

    LOCATION = [xr[1], yr[0]], TICKDIR = 0, TEXTPOS=1, $

    TICKLEN = (0.02*(xr[1] - xr[0])))


oPlotXAxis2 = IDLgrAxis( 0, /EXACT, RANGE = xr, $

    XCOORD_CONV = xs, YCOORD_CONV = ys, $

    LOCATION = [xr[0], yr[1]],/NOTEXT,MAJOR=0,MINOR=0)   


;add axis to the model 

mymodel.Add, oPlotXAxis

mymodel.Add, oPlotYAxis

mymodel.Add, oPlotYAxis2

mymodel.Add, oPlotXAxis2


;draw the view

mywindow.Draw, myview


END

 

The graphic generated will be similar to the following:

 

-----------------------------------------------

created by BC on 2/19/2025

Reviewd by BC (US) on 2/28/2025