4
$\begingroup$

I want to plot multiple data sets having error bands with legends such that the style of error band is also reflected in the corresponding legend. So for e.g. let's say I have the following two data sets.

sinData = Table[{x, Around[Sin[x], 0.1*Sin[x]]}, {x, 0.1, Pi, 0.1}];
cosData = Table[{x, Around[Cos[x], 0.1*Sin[x]]}, {x, 0.1, Pi, 0.1}];

Now I plot them together with error bands and legend

ListPlot[{sinData, cosData}, IntervalMarkers -> "Bands", 
IntervalMarkersStyle -> {Blue, Red}, Joined -> True, 
IntervalMarkersStyle -> Gray, PlotRange -> All, 
PlotLegends -> LineLegend[{"sin", "cos"}], Frame -> True]

enter image description here

Here the problem is the legends do not have the error band. So I have the following two questions based on this problem.

1) Is it possible to show the same plot with legends having a small rectangular band in the same style as the plot where I would have the control over the height of that small rectangular band with the legend line in the middle. So for the above example the sin curve legend should look something like below with adjustable height.

enter image description here

2) Also is it possible to have different types of lined shading within the error band like for e.g. the commanly used are the chequered shading, forward line shading, backward line shading etc. Below I have shown a portion of a plot with chequered shaded error band.

enter image description here

then it should have the corresponding chequered shaded legend

enter image description here

$\endgroup$
8
$\begingroup$

Update 2: We can use the (undocumented) option "LegendItem" for LineLegend as follows:

First create the desired legend glyphs:

legenditems = MapThread[ListLinePlot[Table[{x, Around[1, 0.5]}, {x, 0, 1, 0.1}], 
    IntervalMarkers -> "Bands", PlotStyle -> #, IntervalMarkersStyle -> #2, 
    AspectRatio -> 1, Axes -> False] &, 
  {ColorData[97] /@ {1, 2}, {Blue, Red}}]

enter image description here

and use legenditems as the setting for the option "LegendItem":

 ListLinePlot[{sinData, cosData}, IntervalMarkers -> "Bands", 
  IntervalMarkersStyle -> {Red, Blue}, 
  ImageSize -> Large, PlotRangeClipping -> False, 
  PlotLegends -> LineLegend[{"sinData", "cosData"}, 
   "LegendItem" -> legenditems, LegendMarkerSize -> {50, 50}]]

enter image description here

You can do the same for pattern-filled bands:

patternedlegenditems = MapThread[ListLinePlot[Table[{x, Around[1, 0.5]}, {x, 0, 1, 0.1}], 
    IntervalMarkers -> "Bands", PlotStyle -> #, 
    IntervalMarkersStyle -> PatternFilling[{"Diamond", #2}, ImageScaled[1/5]], 
    AspectRatio -> 1, Axes -> False] &, 
  {ColorData[97] /@ {1, 2}, {Blue, Red}}]

enter image description here

ListLinePlot[{sinData, cosData}, IntervalMarkers -> "Bands", 
  IntervalMarkersStyle -> 
     (PatternFilling[{"Diamond", #}, ImageScaled[1/50]] & /@ {Red, Blue}), 
 ImageSize -> Large, PlotRangeClipping -> False, 
 PlotLegends -> LineLegend[{"sinData", "cosData"}, 
   "LegendItem" -> patternedlegenditems, LegendMarkerSize -> {50, 50}]]

enter image description here

Update: Use the input data Table[{x, Around[1, 0.5]}, {x, 0, 1, 0.1}] to get line legends with bands:

legends2 = MapThread[Labeled[
   ListPlot[Table[{x, Around[1, 0.5]}, {x, 0, 1, 0.1}], 
      IntervalMarkers -> "Bands", 
      PlotStyle -> #, IntervalMarkersStyle -> #2, ImageSize -> 50, 
      AspectRatio -> 1, Joined -> True, Axes -> False, 
      PlotRangeClipping -> False], #3 , Right] &,
    {ColorData[97] /@ {1, 2}, {Blue, Red}, {"sinData", "cosData"}}]

![enter image description here

Use IntervalMarkersStyle -> PatternFilling[{"Diamond", #2}, ImageScaled[1/5]] to get

enter image description here

Original answer:

You can create the desired legend markers by resizing the plots:

plots = MapThread[ListPlot[#, IntervalMarkers -> "Bands", 
            PlotStyle -> #2, IntervalMarkersStyle -> #3,
            Joined -> True, Axes -> False, PlotRangeClipping -> False] &, 
           {{sinData, cosData}, ColorData[97]/@ {1, 2}, {Blue, Red}}]; 

 legends = MapThread[Labeled[Show[#, AspectRatio -> 1, 
             ImageSize -> 40, PlotRange -> All], #2, Right] &, 
         {plots, {"sinData", "cosData"}}];

 Legended[Show[plots, Frame -> True, PlotRange -> All], legends]

enter image description here

Use IntervalMarkersStyle ->PatternFilling[{"Diamond", #3}, ImageScaled[1/50]] to get

enter image description here

$\endgroup$
6
  • $\begingroup$ I may have been unclear. I do not want the legend to look like the plot. The legend always needs to be a line legend but with a band as shown in my question. $\endgroup$ – Hubble07 Jun 14 '20 at 18:56
  • $\begingroup$ @Hubble07, please see the update. $\endgroup$ – kglr Jun 14 '20 at 19:34
  • $\begingroup$ @kglr There seems to be a problem with the code. I think IntervalMarkersStyle -> #3 should be IntervalMarkersStyle -> #2 and the , after the first MapThread should be a ;. $\endgroup$ – Rohit Namjoshi Jun 14 '20 at 19:53
  • $\begingroup$ Thank you @Rohit. Fixed it. $\endgroup$ – kglr Jun 14 '20 at 20:21
  • 1
    $\begingroup$ @kglr Thanks a lot. But I am facing one last issue. When I use IntervalMarkersStyle -> PatternFilling[{"Diamond", #2}, ImageScaled[1/5]] unlike the other case the border for the legend bands become balck. I am unable to change the black color. Where is it getting the information for the border color? Ideally I would like it to have no border, just the pattern would suffice. $\endgroup$ – Hubble07 Jun 16 '20 at 18:01

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.