4
$\begingroup$

Consider:

f[x_, y_] = 6 - 3 x - 2 y;
ContourPlot[f[x, y], {x, -4, 6}, {y, -2, 2},
 ContourShading -> None,
 Contours -> {-6, 0, 6, 12},
 ContourLabels -> 
  Function[{x, y, z}, 
   Text[Framed[z], {x, y}, Background -> LightBlue]],
 Frame -> False,
 Axes -> True, AxesLabel -> {"x", "y"},
 ImagePadding -> All]

Which produces this image:

enter image description here

As you can see, I tried to do some image padding, but not all edges of the text frames on the contour labels are visible.

Any suggestions?

PlotRangeClipping did not quite work:

f[x_, y_] = 6 - 3 x - 2 y;
ContourPlot[f[x, y], {x, -4, 6}, {y, -2, 2},
 ContourShading -> None,
 Contours -> {-6, 0, 6, 12},
 ContourLabels -> 
  Function[{x, y, z}, 
   Text[Framed[z], {x, y}, Background -> LightBlue]],
 Frame -> False,
 Axes -> True, AxesLabel -> {"x", "y"},
 PlotRangeClipping -> False]

enter image description here

Ok on the top, but not on the bottom.

PlotRangePadding worked:

f[x_, y_] = 6 - 3 x - 2 y;
ContourPlot[f[x, y], {x, -4, 6}, {y, -2, 2},
 ContourShading -> None,
 Contours -> {-6, 0, 6, 12}, 
 ContourLabels -> 
  Function[{x, y, z}, 
   Text[Framed[z], {x, y}, Background -> LightBlue]],
 Frame -> False,
 Axes -> True, AxesLabel -> {"x", "y"},
 PlotRangePadding -> Scaled[.05]]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Try PlotRangePadding -> 1 instead of ImagePadding $\endgroup$ – Dr. belisarius Jul 8 '15 at 18:58
  • $\begingroup$ @belisarius I used PlotRangePadding->Scaled[0.5] from the popup menu and it now looks nice. Thanks for the help. $\endgroup$ – David Jul 8 '15 at 19:14
  • 2
    $\begingroup$ The other option to try is PlotRangeClipping->False. $\endgroup$ – N.J.Evans Jul 8 '15 at 19:57
  • $\begingroup$ @N.J.Evans. Did not quite work. See update to my original post question. Sorry I didn't include the function originally. Thanks for the reply. $\endgroup$ – David Jul 8 '15 at 23:01
  • 3
    $\begingroup$ Concerning your disappointment with PlotRangeClipping -> False -- you should have used PlotRange -> All, PlotRangeClipping -> False $\endgroup$ – m_goldberg Jul 9 '15 at 4:08
3
$\begingroup$

The expressions inset by Text are not considered in the PlotRange used by ContourPlot. We can make the problem more severe and apparent by using wider FrameMargins:

f[x_, y_] := 6 - 3 x - 2 y;

gr = ContourPlot[f[x, y], {x, -4, 6}, {y, -2, 2}, ContourShading -> None, 
  Contours -> {-6, 0, 6, 12}, 
  ContourLabels -> 
   Function[{x, y, z}, 
    Inset[Framed[z, FrameMargins -> 30], {x, y}, Background -> LightBlue]], 
  Frame -> False, Axes -> True, AxesLabel -> {"x", "y"}]

enter image description here

By resetting PlotRange -> All and turning off PlotRangeClipping we can include these labels:

Show[gr, PlotRange -> All, PlotRangeClipping -> False]

enter image description here

In this case the axis label ends up inside one of the frames because the axes are not extended. You could add a Spacer to the y label to improve this. If you use PlotRangePadding instead the axes expand and put the labels further out, but there is no direct way to set the value you need automatically.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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