5

I'm wondering if there is any way to manually create gaps for the contour lines so that the contour labels can be put in the middle of these gaps.

I try to make contour labels in the style like the following ones.

enter image description here

enter image description here

Here is a toy example from the Mathematica online manual.

ListContourPlot[Table[Sin[x] Sin[y], {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], ContourLabels -> All]

Could you please share you solutions for making the "Gap-style" contour labels in a color plot? Many thanks!

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Frankie
  • 605
  • 3
  • 7

1 Answers1

6

enter image description here

A bit kludgy:

    SetOptions[ContourPlot, Frame -> None];
    f[x_, y_] := x/Exp[x^2 + y^2]; (* plug in your function *)
theColors = 
     ContourPlot[f[x, y], {x, -2, 2}, {y, -2, 2},
      ContourStyle -> None];
theContours = 
      ContourPlot[f[x, y], {x, -2, 2}, {y, -2, 2}, 
       ContourLabels -> (Text[
           Framed[" ", FrameStyle -> Directive[White]], {#1, #2}, 
           Background -> White] &), 
       ColorFunction -> Function[{x, y}, White]];
theLabels = 
      ContourPlot[f[x, y], {x, -2, 2}, {y, -2, 2}, ContourLabels -> All, 
       ColorFunction -> Function[None], ContourStyle -> None];
ImageMultiply[ImageMultiply[theColors, theContours], theLabels]
David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • Hi David, your plot looks pretty good. However, when I try to export this figure with the codes, the codes give me a very low-resolution result no matter how I set the resolution option. I'll give it a try on other machines. I don't know whether or not "ImageMutiply" will return us a raster result. – Frankie Aug 02 '21 at 16:16
  • 1
    Try Rasterize with RasterSize->1000. – David G. Stork Aug 02 '21 at 16:57