2
$\begingroup$

How can I add contour labels to contour lines? The usual ContourLabels->Automatic doesn't seem to be working when the input is given in the third form of ContourPlot.

labelFontSize = 24;
ticksFontSize = 16;
lineThickness = 2;

contourPlotToF = ContourPlot[
tArrival - tDeparture == Table[Δt, {Δt, 0, 70, 7}],
{tDeparture, 30, 60}, {tArrival, 30, 60},
ImageSize -> Full,
FrameLabel -> {Row[{"Departure time/", Quantity[None, "Days"]}], 
Row[{"Arrival time/", Quantity[None, "Days"]}]},
FrameTicksStyle -> Directive[FontSize -> ticksFontSize],
LabelStyle -> {FontSize -> labelFontSize},
ContourLabels -> Automatic,
ContourStyle -> {Directive[
  ColorData[97, "ColorList"][[4]],
  AbsoluteThickness[lineThickness]]},
GridLines -> Automatic
]

Edit:

As per @Michael E2 suggestion, I've altered

tArrival - tDeparture == Table[Δt, {Δt, 0, 70, 7}],

to

Evaluate[Thread[tArrival-tDeparture==Table[Δt,{Δt, 0, 70, 7}]]],

which is producing the following result: Contour plot

The label is appearing only when I hover the line, and just giving the list of contours. What I use for "traditional" contour plots is

ContourLabels -> (Text[Style[#3, ticksFontSize], {#1, #2}] &),

for the labels to appear next to the lines. However, this doesn't seem to work here.

PS: By "traditional" contour plots, I mean of the first form of ContourPlot, with the colour coded in-between contours.

$\endgroup$
6
  • 2
    $\begingroup$ The third form, strictly speaking, is given by ContourPlot[Evaluate[Thread[tArrival - tDeparture == Table[\[CapitalDelta]t, {\[CapitalDelta]t, 0, 70, 7}]]],...]. The labels and styling of plots come from the given form, before it is processed for plotting. $\endgroup$
    – Michael E2
    Sep 20, 2020 at 16:54
  • $\begingroup$ @BobHanlon That way I get a "traditional" contour plot, whereas I want only the lines... $\endgroup$
    – Jak
    Sep 21, 2020 at 11:57
  • $\begingroup$ @MichaelE2 Thank you for the correction. Still, the labels are only appearing when I hover, and are the same for every contour (I edited the question to include what I'm getting). Is it possible to add each contour value next to the line? The code I use for "traditional" contour plots does not work here (also included in the edit). $\endgroup$
    – Jak
    Sep 21, 2020 at 12:03
  • 1
    $\begingroup$ When the input is in the form of an equation, either the second or third paradigm in the docs, ContourPlot treats the equation as the label. Since the numerical value on each side of an equation may vary along the solution curve, the value is not treated as significant. I think @Bob's approach can probably be adapted to your needs more easily than using the third form. $\endgroup$
    – Michael E2
    Sep 21, 2020 at 13:04
  • 2
    $\begingroup$ Related: mathematica.stackexchange.com/questions/230494/… $\endgroup$
    – Michael E2
    Sep 21, 2020 at 13:43

1 Answer 1

1
$\begingroup$

Alternatively, use the first form

Clear["Global`*"]

labelFontSize = 24;
ticksFontSize = 16;
lineThickness = 2;

ContourPlot[tArrival - tDeparture,
 {tDeparture, 30, 60}, {tArrival, 30, 60},
 Contours -> Range[0, 70, 7],
 ImageSize -> Medium,
 FrameLabel -> {
   Row[{"Departure time/", Quantity[None, "Days"]}],
   Row[{"Arrival time/", Quantity[None, "Days"]}]}, 
 FrameTicksStyle -> Directive[FontSize -> ticksFontSize], 
 LabelStyle -> {FontSize -> labelFontSize},
 ContourLabels ->
  (Text[Framed[#3], {#1, #2}, Background -> White] &), 
 ContourStyle -> 
  Directive[ColorData[97, "ColorList"][[4]], AbsoluteThickness[lineThickness]],
 ContourShading -> None,
 GridLines -> Automatic]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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