There have been several posts, over the years, related to visual cues about the values associated with particular 2D contours in a plot.

Some people ask or post about color-bars [1]. Some people ask or post about inlined labelling of the curves [1, 2, 3, 4, 5, 6, 7]. And some post about mouse popup/hover-over functionality [1]., which got added as general new 2D plot annotation functionality in Maple 2017 and is available for the plots:-contourplot command via its contourlabels option.

Another possibility consists of a legend for 2D contour plots, with distinct entries for each contour value. That is not currently available from the plots:-contourplot command as documented. This post is about obtaining such a legend.

Aside from the method used below, a similar effect may be possible (possibly with a little effort) using contour-plotting approaches based on individual plots:-implicitplot calls for each contour level. Eg. using Kitonum's procedure, or an undocumented, alternate internal driver for plots:-contourplot.

Since I like the functionality provided by the contourlabels option I thought that I'd highjack that (and the _HOVERCONTENT plotting substructure that plot-annotations now generate) and get a relatively convenient way to get a color-key via the 2D plotting legend.  This is not supposed to be super-efficient.

Here below are some examples. I hope that it illustrates some useful functionality that could be added to the contourplot command. It can also be used to get a color-key for use with densityplot.

restart;

contplot:=proc(ee, rng1, rng2)
  local clabels, clegend, i, ncrvs, newP, otherdat, others, tcrvs, tempP;
  (clegend,others):=selectremove(type,[_rest],identical(:-legend)=anything);
  (clabels,others):= selectremove(type,others,identical(:-contourlabels)=anything);
  if nops(clegend)>0 then
    tempP:=:-plots:-contourplot(ee,rng1,rng2,others[],
                                ':-contourlabels'=rhs(clegend[-1]));
    tempP:=subsindets(tempP,'specfunc(:-_HOVERCONTENT)',
                      u->`if`(has(u,"null"),NULL,':-LEGEND'(op(u))));
    if nops(clabels)>0 then
      newP:=plots:-contourplot(ee,rng1,rng2,others[],
                              ':-contourlabels'=rhs(clabels[-1]));
      tcrvs:=select(type,[op(tempP)],'specfunc(CURVES)');
      (ncrvs,otherdat):=selectremove(type,[op(newP)],'specfunc(CURVES)');
      return ':-PLOT'(seq(':-CURVES'(op(ncrvs[i]),op(indets(tcrvs[i],'specfunc(:-LEGEND)'))),
                          i=1..nops(ncrvs)),
                      op(otherdat));
    else
      return tempP;
    end if;
  elif nops(clabels)>0 then
    return plots:-contourplot(ee,rng1,rng2,others[],
                              ':-contourlabels'=rhs(clabels[-1]));
  else
    return plots:-contourplot(ee,rng1,rng2,others[]);
  end if;
end proc:
 

contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 9,
      size=[500,400],
      legendstyle = [location = right],
      legend=true,
      contourlabels=true,
      view=[-2.1..2.1,-2.1..2.1]
);

contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 17,
      size=[500,400],
      legendstyle = [location = right],
      legend=['contourvalue',$("null",7),'contourvalue',$("null",7),'contourvalue'],
      contourlabels=true,
      view=[-2.1..2.1,-2.1..2.1]
);

# Apparently legend items must be unique, to persist on document re-open.

contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 11,
      size=[500,400],
      legendstyle = [location = right],
      legend=['contourvalue',seq(cat($(` `,i)),i=2..5),
              'contourvalue',seq(cat($(` `,i)),i=6..9),
              'contourvalue'],
      contourlabels=true,
      view=[-2.1..2.1,-2.1..2.1]
);

contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Green","Red"],
      contours = 8,
      size=[400,450],
      legend=true,
      contourlabels=true
);

contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 13,
      legend=['contourvalue',$("null",5),'contourvalue',$("null",5),'contourvalue'],
      contourlabels=true
);

(low,high,N):=0.1,7.6,23:
conts:=[seq(low..high*1.01, (high-low)/(N-1))]:
contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = conts,
      legend=['contourvalue',$("null",floor((N-3)/2)),'contourvalue',$("null",ceil((N-3)/2)),'contourvalue'],
      contourlabels=true
);

plots:-display(
  subsindets(contplot((x^2+y^2)^(1/2), x=-2..2, y=-2..2,
                      coloring=["Yellow","Blue"],
                      contours = 7,
                      filledregions),
             specfunc(CURVES),u->NULL),
  contplot((x^2+y^2)^(1/2), x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 7, #grid=[50,50],
      thickness=0,
      legendstyle = [location=right],
      legend=true),
  size=[600,500],
  view=[-2.1..2.1,-2.1..2.1]
);

 

plots:-display(
  contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 5,
      thickness=0, filledregions),
  contplot(x^2+y^2, x=-2..2, y=-2..2,
      coloring=["Yellow","Blue"],
      contours = 5,
      thickness=3,
      legendstyle = [location=right],
      legend=typeset("<=",contourvalue)),
  size=[700,600],
  view=[-2.1..2.1,-2.1..2.1]
);

N:=11:
plots:-display(
  contplot(sin(x)*y, x=-2*Pi..2*Pi, y=-1..1,
      coloring=["Yellow","Blue"],
      contours = [seq(-1+(i-1)*(1-(-1))/(N-1),i=1..N)],
      thickness=3,
      legendstyle = [location=right],
      legend=true),
   plots:-densityplot(sin(x)*y, x=-2*Pi..2*Pi, y=-1..1,
      colorscheme=["zgradient",["Yellow","Blue"],colorspace="RGB"],
      grid=[100,100],
      style=surface, restricttoranges),
   plottools:-line([-2*Pi,-1],[-2*Pi,1],thickness=3,color=white),
   plottools:-line([2*Pi,-1],[2*Pi,1],thickness=3,color=white),
   plottools:-line([-2*Pi,1],[2*Pi,1],thickness=3,color=white),
   plottools:-line([-2*Pi,-1],[2*Pi,-1],thickness=3,color=white),
   size=[600,500]
);

N:=13:
plots:-display(
  contplot(sin(x)*y, x=-2*Pi..2*Pi, y=-1..1,
      coloring=["Yellow","Blue"],
      contours = [seq(-1+(i-1)*(1-(-1))/(N-1),i=1..N)],
      thickness=6,
      legendstyle = [location=right],
      legend=['contourvalue',seq(cat($(` `,i)),i=2..3),
              'contourvalue',seq(cat($(` `,i)),i=5..6),
              'contourvalue',seq(cat($(` `,i)),i=8..9),
              'contourvalue',seq(cat($(` `,i)),i=11..12),
              'contourvalue']),
   plots:-densityplot(sin(x)*y, x=-2*Pi..2*Pi, y=-1..1,
      colorscheme=["zgradient",["Yellow","Blue"],colorspace="RGB"],
      grid=[100,100],
      style=surface, restricttoranges),
   plottools:-line([-2*Pi,-1],[-2*Pi,1],thickness=6,color=white),
   plottools:-line([2*Pi,-1],[2*Pi,1],thickness=6,color=white),
   plottools:-line([-2*Pi,1],[2*Pi,1],thickness=6,color=white),
   plottools:-line([-2*Pi,-1],[2*Pi,-1],thickness=6,color=white),
  size=[600,500]
);

 

Download contour_legend_post.mw

 

 

 


Please Wait...