4
$\begingroup$

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.

enter image description here

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.

$\endgroup$
  • 1
    $\begingroup$ …and where can people who are not you obtain new.dat? $\endgroup$ – J. M. is away Jul 25 '16 at 13:33
  • $\begingroup$ @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? $\endgroup$ – baban Jul 25 '16 at 13:38
  • $\begingroup$ @baban just Google "upload file" and pick your host. There are lots of free ones out there. $\endgroup$ – Feyre Jul 25 '16 at 13:39
  • $\begingroup$ Here is the link for the data file. dropbox.com/s/5iluqr3jmfzb6oz/new.dat?dl=0 $\endgroup$ – baban Jul 25 '16 at 13:43
  • $\begingroup$ why not use PlotLegends? $\endgroup$ – Sumit Jul 25 '16 at 14:37
3
$\begingroup$

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.

enter image description here

$\endgroup$
  • $\begingroup$ What a ugly way! But it works brutally! :P $\endgroup$ – Jake Pan Aug 23 '18 at 5:53
1
$\begingroup$
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]

enter image description here

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]

enter image description here

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])]

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – baban Jul 25 '16 at 15:02
  • $\begingroup$ Writing individual labels might be problematic if you have so many contours. And for MMA8 check this. $\endgroup$ – Sumit Jul 25 '16 at 15:13
  • $\begingroup$ Ya Thanks! So, then there is no way where I can desirably place my contour labels in the contour plot? $\endgroup$ – baban Jul 25 '16 at 15:17
  • $\begingroup$ @baban, check my new answer. $\endgroup$ – Sumit Jul 25 '16 at 16:03
  • $\begingroup$ Great! This is nice. Thank you! $\endgroup$ – baban Jul 25 '16 at 16:16

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.