# Aligning Density an Contour Graphics

In radiation therapy, we display radiation dose as an isodose contour plot overlain on density plot describing the patient geometry (CT data). Here is a simple example (the material grid is voxelated and appears in grayscale & the isodose lines are shown in color overlaying the geometry voxels):

You can see that the left and right sides of the symmetric geometry are placed differently.

In Mathematica, i would like to show a ListContourPlot over a ListDensityPlot (or a Raster, or an ArrayPlot or a ListContourPlot with InterpolationOrder->0). The challenge, is that the higher interpolation orders do not align with the zero order. Here is a simple example, where the same array might be expected to align using the ListDensityPlot and ListContourPlot. But this does not happen because the Density Plot is being done with interpolation order of zero.

iMax = 10;
plotData = Table[If[x == 5 || y == 5, 1, 0], {x, 1, iMax}, {y, 1, iMax}];
Show[{
ListDensityPlot[plotData
, InterpolationOrder -> 0
, ColorFunction -> GrayLevel
]
,ListContourPlot[plotData
, InterpolationOrder -> 1
, Contours -> 1
, ContourStyle -> {Red}
]
}]


Which results in:

My goal is to have the red contours centered on the white white cross. I have tried using Inset and Raster as well as overlaying two ListContourPlots. Each approach has it's own challenges. I would prefer to keep the List plotting functions for speed and not have to create interpolating functions of the dose and geometry.

Any suggestions for strategies to get these plots aligned?

Thank you!

Looking at the same code with first order interpolation:

iMax = 10;
plotData =
Table[If[x == 5 || y == 5, 1, 0], {x, 1, iMax}, {y, 1, iMax}];
Show[{
ListContourPlot[plotData
, InterpolationOrder -> 1
, ColorFunction -> GrayLevel
]
, ListContourPlot[plotData
, InterpolationOrder -> 1
, Contours -> {0.5}
, ContourStyle -> {{Thick, Red}}
]
}]


Results in the image below:

The material grid (which are cubes) are now shown in a manner which does not cover their true extent.

• Why not just use the ContourShading in ContourPlot and get rid of the density plot altogether? Try getting rid of ContourShading -> False and add ColorFunction -> GrayLevel. Jul 6, 2015 at 2:27
• I did try that. It's still a challenge to get the alignment and geometry to appear and align correctly. The background plot, the geometry, still has to be a zero order interpolation. Otherwise the geometry gets all blurred out. This requirement seems top be what causes the misalignment. Jul 6, 2015 at 2:54

If you can include the $x$ and $y$ coordinates along with the data, that can fix the problem. For instance, in your example, take the example data to be

iMax = 10;
plotData = Flatten[
Table[{x, y, If[x == 5 || y == 5, 1, 0]}
, {x, 1, iMax}, {y, 1, iMax}]
, 1]


Then,

Show[{
ListDensityPlot[plotData,
InterpolationOrder -> 0, ColorFunction -> GrayLevel
],
ListContourPlot[plotData
, InterpolationOrder -> 1
, Contours -> 1
, ContourStyle -> {Opacity[1], Thickness[0.01], Red}
]
}]


yields

• Splendid! That seems to work in this and other cases as well! Awesome! I can also try extend it to the cases where the data spacing is not even-spaced. Jul 6, 2015 at 13:40

Here is a simple way to do the superimposed plots, using ArrayPlot:

Show[{ArrayPlot[plotData, DataReversed -> True,
ColorFunction -> GrayLevel, DataRange -> {{0, 10}, {0, 10}}],
ListContourPlot[plotData, DataRange -> {{0, 10}, {0, 10}},
InterpolationOrder -> 1, Contours -> 1, ContourShading -> False,
ContourStyle -> {Red}]}]


The DataRange specifications are crucial to aligning the plots, because the default coordinates for lists start with 1.

• Although this solution was also valid, I chose the other one because it extended easily into the cases of un-evenly spaced data points. Thank you though! Jul 6, 2015 at 13:46