2
$\begingroup$

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): enter image description here

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
    , ContourShading -> False
    , ContourStyle -> {Red}
  ]
}]

Which results in:

enter image description here

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}
  , ContourShading -> False
  , ContourStyle -> {{Thick, Red}}
  ]
}]

Results in the image below:

enter image description here

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

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – march
    Jul 6, 2015 at 2:27
  • $\begingroup$ 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. $\endgroup$
    – xsk8rat
    Jul 6, 2015 at 2:54

2 Answers 2

5
$\begingroup$

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
    , ContourShading -> False
    , ContourStyle -> {Opacity[1], Thickness[0.01], Red}
  ]
 }]

yields

enter image description here

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – xsk8rat
    Jul 6, 2015 at 13:40
5
$\begingroup$

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}]}]

plot2

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

$\endgroup$
1
  • $\begingroup$ 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! $\endgroup$
    – xsk8rat
    Jul 6, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.