Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a plot that includes a Region Plot. I want to make a rectangle with the same shading as in the region plot to show as a legend. The dumb way I can think off is to make another region plot over a small rectangular space and show that as a legend, but that does not seem like a great idea considering I might want to change the plots.

Any better suggestions?

For practice, I want to label the following example:

RegionPlot[x^2 + y^3 < 2, {x, -2, 2}, {y, -2, 2}, 
  MeshFunctions -> {(2 #1 - #2) &, (2 #1 - #2) &}, Mesh -> 100]
share|improve this question
2  
Please see the answer here: Mesh inside Plot legend –  rm -rf Feb 9 at 4:49
add comment

1 Answer

up vote 7 down vote accepted

Perhaps a "quick" appoach (in addition to looking at hyperlink of @rm-rf):

msp[f_, {xmin_, xmax_}, {ymin_, ymax_}, mfun_, mnum_, swatchsize_, 
  opts : OptionsPattern[]] := Module[{pm, swl},
  pm = RegionPlot[x < 1, {x, 0, 1}, {y, 0, 1}, MeshFunctions -> mfun, 
    Mesh -> IntegerPart@(mnum/(swatchsize/10)), Frame -> False, 
    PlotRange -> {{0, 1}, {0, 1}}, 
    Evaluate@FilterRules[{opts}, Options[RegionPlot]], 
    ImageSize -> swatchsize];
  swl = SwatchLegend["Expressions", LegendMarkers -> pm, 
    LegendMarkerSize -> swatchsize];
  RegionPlot[f, {x, xmin, xmax}, {y, ymin, ymax}, 
   MeshFunctions -> mfun, Mesh -> mnum, 
   Evaluate@FilterRules[{opts}, Options[RegionPlot]], 
   PlotLegends -> swl]]

Some examples:

msp[x^2 + y^3 < 2, {-2, 2}, {-2, 2}, {#1 &, #2 &}, 50, 50]

enter image description here

msp[x^2 + y^3 < 2, {-2, 2}, {-2, 2}, {#1 + #2 &, #1 - #2 &}, 50, 50]

enter image description here

msp[x^2 + y^3 < 2, {-2, 2}, {-2, 2}, {#1 &, #2 &}, 50, 50, 
 PlotStyle -> Yellow, BoundaryStyle -> {Thick, Purple}]

enter image description here

UPDATE

mspmod[f_, {xmin_, xmax_}, {ymin_, ymax_}, mfun_, mnum_, swatchsize_, 
  col_, opts : OptionsPattern[]] := 
 Module[{pm, swl}, 
  pm = RegionPlot[x < 1, {x, 0, 1}, {y, 0, 1}, MeshFunctions -> mfun, 
      Mesh -> IntegerPart@(mnum/(swatchsize/10)), Frame -> False, 
      PlotRange -> {{0, 1}, {0, 1}}, PlotStyle -> #, 
      Evaluate@FilterRules[{opts}, Options[RegionPlot]], 
      ImageSize -> swatchsize] & /@ col;
  swl = SwatchLegend["Expressions", LegendMarkers -> pm, 
    LegendMarkerSize -> swatchsize];
  RegionPlot[f, {x, xmin, xmax}, {y, ymin, ymax}, 
   MeshFunctions -> mfun, Mesh -> mnum, PlotStyle -> col, 
   Evaluate@FilterRules[{opts}, Options[RegionPlot]], 
   PlotLegends -> swl]]

Example:

mspmod[{x^3 - y^2 > 2, x^2 + y^3 > 3, 
  x^3 - y^2 > 2 && x^2 + y^3 > 3}, {-3, 3}, {-3, 3}, 50, 50, {Red, 
  Green, Yellow}]

enter image description here

share|improve this answer
    
I tried to make improvements to make this valid for two functions, but the coloring of the legend is always just one of them. Any suggestions on how to make msp accept multiple functions? Worst case, I'll just use Show. Thanks! –  Lina Feb 16 at 1:07
    
@Lina , I suspect Show is simplest way. Will think about when I get time. Hyperlink already referred in other comments may give inspiration. –  ubpdqn Feb 16 at 3:27
    
@Lina see update –  ubpdqn Feb 16 at 7:56
1  
@Lina for some reason the update I made disappeared so I reupdated. –  ubpdqn Feb 19 at 12:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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