Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Jens Nöckel has write functions about (List) Contour Plots with rasterized shading in Mathematica:

It works well. But in Mathematica 9 it can not work with PlotLegends. How to modify? I can use Grid to combine a result of rasterListContourPlot with a legend, but this lost the link of the graphic with the legend.

rasgrap = rasterListContourPlot[RandomReal[1, {10, 10}], InterpolationOrder -> 3]
mylegend = BarLegend["LakeColors"];

Grid[{{rasgrap, mylegend}}]

rasterListContourPlot can be found in the above link.

share|improve this question
The reason my functions don't work with version 9 legends is of course that they were written before version 9. And unfortunately the new legends cause the output of plots to have different Head (Legended instead of Graphics) when you specify a legend. I didn't anticipate this new invention (nor do I think it's a good change). But I'll try to modify my functions eventually. This kind of thing is exactly why I've mainly kept working with version 8 so far. It's too much work to update all my notebooks... – Jens May 5 '13 at 17:30
I've changed the code at the URL linked above, so the problem mentioned in the question no longer occurs. If the changes work as I hope, it may be best to close this question as too localized. But I'd suggest waiting a little so I can get feedback. – Jens May 5 '13 at 19:31

The question refers to a function rasterListContourPlot whose purpose was to replace the polygon-based filling of ListContourPlot with a rasterized image while maintaining all line-bases primitives as vector art. The output is a combination of Graphics objects, and there was no provision for Legended wrappers.

Here is a quick fix to the particular function mentioned in the question. It works for the example you're using, and I'll test it on some other examples. Please let me know if this works for you:

rasterListContourPlot[pList_, opts : OptionsPattern[]] := 
 Module[{img, cont, contL, plotRangeRule, contourOptions, 
   frameOptions, rangeCoords}, 
  contourOptions = 
      Except[{Background, Frame, Axes}]]], {Frame -> None, 
     Axes -> None}];
  contL = 
   ListContourPlot[pList, Evaluate@Apply[Sequence, contourOptions]];
  cont = First[Cases[{contL}, Graphics[__], Infinity]];
  img = Rasterize[
    Graphics[GraphicsComplex[cont[[1, 1]], cont[[1, 2, 1]]], 
     PlotRangePadding -> None, ImagePadding -> None, 
     Options[cont, PlotRange]], "Image", 
    ImageSize -> 
     With[{size = 
        Total[{2, 0} (ImageSize /. {opts}) /. {ImageSize -> 
      If[NumericQ[size], size, 
       First[WindowSize /. Options[EvaluationNotebook[]]]]]];
  plotRangeRule = FilterRules[Quiet@AbsoluteOptions[cont], PlotRange];
  rangeCoords = Transpose[PlotRange /. plotRangeRule];
  frameOptions = 
      Except[{PlotRangeClipping, PlotRange}]]], {plotRangeRule, 
     Frame -> True, PlotRangeClipping -> True}];
  If[Head[contL] === Legended, Legended[#, contL[[2]]], #] &@
         "ShadingOpacity" /. {opts} /. {"ShadingOpacity" -> 1}], 
        AspectRatio -> Full], rangeCoords[[1]], {0, 0}, 
       rangeCoords[[2]] - rangeCoords[[1]]]}, 
     PlotRangePadding -> None], 
    Graphics[GraphicsComplex[cont[[1, 1]], cont[[1, 2, 2]]]], 
    Evaluate@Apply[Sequence, frameOptions]]]

rasterListContourPlot[RandomReal[1, {10, 10}], 
 InterpolationOrder -> 3, PlotLegends -> Automatic]


What I did is to put in a Cases that filters out the actual Graphics on which I then do the manipulations as before, without any changes. In the end, an If statement is used to add the Legended wrapper back if it existed beforehand.

share|improve this answer
Thank jens very much! it works fine in my case. – jiuyuxinshi May 6 '13 at 17:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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