6
$\begingroup$

I have a data set with entries of the form [nx,ny,E] and have made a contour plot of this data. I would like to add labels to only a subset of these contours, for instance every other contour. How might I go about doing that?

My data set is derived from a calculation, and so my data points are not samples of a function with two arguments. I have made an example code below:

data = Table[{m/100, n/100, (m^2 + n^2)/20000}, {m, 0, 100}, {n, 0, 100}];
data = Flatten[data, 1];
contour = Range[0, 1, 0.05];
contourplot = ListContourPlot[data, Axes -> True, Contours -> contour, ContourLabels -> All, ContourShading -> None]
$\endgroup$
  • $\begingroup$ possible duplicate of Custom contour labels in ContourPlot $\endgroup$ – Jens Nov 14 '14 at 18:35
  • 2
    $\begingroup$ An answer in the proposed duplicate mentions the placement of the labels may require that you solve for the root (contour-value) of your expression; might this be a problem for the OP's data, which is plotted with ListContourPlot? $\endgroup$ – bobthechemist Nov 14 '14 at 21:14
5
$\begingroup$
data = Table[{m/100, n/100, (m^2 + n^2)/20000}, {m, 0, 100}, {n, 0,  100}];
data = Flatten[data, 1];
contour = Range[0, 1, 0.05];

lbldcont = Range[0, 1, 0.05][[{1, 3, 5, 12, 15, 20}]]; 
(* {0., 0.1, 0.2, 0.55, 0.7, 0.95} *)

ListContourPlot[data, Axes -> True, Contours -> Range[0, 1, 0.05], ContourShading -> None,
 ContourLabels -> (If[MemberQ[lbldcont, #3],Text[#3, {#1, #2}, Background -> Transparent]] &)]

enter image description here

$\endgroup$
  • $\begingroup$ That was fast! That works perfectly, thanks very much! $\endgroup$ – n-ism Nov 14 '14 at 14:49
  • $\begingroup$ @n-ism, my pleasure. Thank you for the accept... And welcome to mma.se. $\endgroup$ – kglr Nov 14 '14 at 17:10
4
$\begingroup$

Quick fix:

contourplot /. x : {__Text} :> x[[;; ;; 2]]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for this solution! I tried using it on my original notebook and found that it did not remove every other contour label like it did with the example I gave. The labels it did remove did make the plot more legible though. $\endgroup$ – n-ism Nov 14 '14 at 14:56

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.