# How to add Contour labels in the desired place?

I have a contour plot as shown below. What I want is to have distinguishable contour labels i.e, here the value of the contours is not readable above 800 or so. How can I place the contour labels in some other positions so that I can mark the value for each of the contour lines? I have looked at already into these types of questions but mostly people have solved it for some functions whereas I have a data file.

I wrote the following code to generate the figure.

c = Import[
"/home/users/chakrtdm/Desktop/Gibbs/cal/analytic_debye/contour_\
plots/TD_DE_T/new.dat", "Table"];
ListContourPlot[c, Contours -> Range[100, 1900, 100],
PlotRange -> {{-10, -100}, {0.01, 0.05}}, PlotRangeClipping -> False,
FrameLabel -> {Style["X"], Style["Y"]}, Background -> White,
AxesStyle -> Thick, FrameStyle -> Directive[Black, 15],
BaseStyle -> {FontSize -> 12}, ColorFunction -> "Rainbow",
ContourLabels -> (Text[Framed[#3], {#1, #2},
Background -> LightBlue] &), ContourStyle -> Opacity[10.0],
ImageSize -> Automatic]


The new.dat file can be found here.

• …and where can people who are not you obtain new.dat? Jul 25 '16 at 13:33
• @J.M.: I am extremely sorry, but it is a little bit big file. Is it somehow possible to attach that file rather copy & paste the entire data here? Jul 25 '16 at 13:38
• @baban just Google "upload file" and pick your host. There are lots of free ones out there. Jul 25 '16 at 13:39
• Here is the link for the data file. dropbox.com/s/5iluqr3jmfzb6oz/new.dat?dl=0 Jul 25 '16 at 13:43
• why not use PlotLegends? Jul 25 '16 at 14:37

I found a very simple solution without thinking much about writing codes for placing contour labels. I find this is extremely helpful for the people who are not comfortable in writing explicit Mathematica codes like me.

The solution is here: Just select the plot and then double click on the plot and then double click on the text you want to make changes (please note that you legend box will be active now and you can do any kind of editing) to move and just drag it in your desired place.

If you want to write something new just type t on the plot and you can write anything you want. After this one has to click outside the graphics to see the changes. This simple method gives me enormous flexibility more specifically on legends. Now my plot looks as shown below which was what I exactly wanted.

• What a ugly way! But it works brutally! :P Aug 23 '18 at 5:53
• Holy crap, thank you. You can also ctrl-c ctrl-v labels if you have complicated curves and need multiple labels on the same curve! Dec 5 '19 at 9:50
c = Import["new.dat", "Table"];
ListContourPlot[c, Contours -> Range[100, 1900, 100],
PlotRange -> {{-10, -100}, {0.01, 0.05}}, PlotRangeClipping -> False,
FrameLabel -> {Style["X"], Style["Y"]}, Background -> White,
AxesStyle -> Thick, FrameStyle -> Directive[Black, 15],
BaseStyle -> {FontSize -> 12}, ColorFunction -> "Rainbow",
PlotLegends -> Automatic]


Alternative Approach

To make it clear I am going to do it with 5 contours.

conts = Range[200, 2000, 400];

ListContourPlot[c, PlotRange -> {{-10, -100}, {0.01, 0.05}},  ColorFunction -> "Rainbow",
PlotLegends -> Automatic, Contours -> conts, ContourLabels -> All, BaseStyle -> 12]


Now you choose a position for your labels

pos = Table[{-60, j}, {j, 0.02, 0.04, 0.005}]


and a nearby reference point on each contour

ref = {{-11, 0.04}, {-11, 0.02}, {-20, 0.04}, {-40, 0.03}, {-100, 0.03}};


And then

plot = ListContourPlot[c, PlotRange -> {{-10, -100}, {0.01, 0.05}},
Contours -> conts, ColorFunction -> "Rainbow"];
lines = Cases[plot // Normal, Line[x_] :> x, Infinity];
cen = Nearest[lines[[#]], ref[[#]]][[1]] & /@ Range[nc];
Show[plot, FrameLabel -> {Style["X"], Style["Y"]},
Background -> White, AxesStyle -> Thick,
FrameStyle -> Directive[Black, 15], BaseStyle -> {FontSize -> 12},
Epilog -> ({Arrow[{pos[[#]], cen[[#]]}],
Text[Style[conts[[nc - # + 1]], 16, Background -> LightBlue],
pos[[#]]]} & /@ Range[1, nc])]


• I think I already have it. My question was regarding the contour labels .What I want is to have the contour labels clearly visible for each of the contour lines. Bar is ok but if you look at my picture I have contour values written in framed texts. But they were really close for above 800 values and I want to tag them in a different way so that each of those can be easily seen. Jul 25 '16 at 15:02
• Writing individual labels might be problematic if you have so many contours. And for MMA8 check this. Jul 25 '16 at 15:13
• Ya Thanks! So, then there is no way where I can desirably place my contour labels in the contour plot? Jul 25 '16 at 15:17
• @baban, check my new answer. Jul 25 '16 at 16:03
• Great! This is nice. Thank you! Jul 25 '16 at 16:16