3
$\begingroup$

I have a plot in which a function is plotted as dotted line in some domain and solid in another, like this:

function=Plot[{Piecewise[{{x+10,x<0}},None],Piecewise[{{x+10,x>=0}},None]},{x,-9,9},PlotStyle->{Dotted,Black}]

Since I'm plotting multiple functions at the same time I'm using Show and Epilog. For this particular function, I would like the line in the legend to be half dotted and half solid as well (rather than just dotted as below):

Show[function, 
Epilog->Inset[Framed[Column[{LineLegend[Thread[Directive[{Black},{Dotted}]],{Style["Function",Medium]},LegendMargins->7]}],RoundingRadius->1],{-5,15.5},{0,0},50]]

How can I can make this happen?

Edit:

Screenshot 1

enter image description here

Screenshot 2 enter image description here

$\endgroup$
1
  • $\begingroup$ Not sure why it doesn't work. I will ask the community to please verify it as I see this on my machine. $\endgroup$
    – Syed
    Mar 12, 2022 at 7:49

2 Answers 2

7
$\begingroup$

Edit

Clear["Global`*"];
plot1 = Plot[{Piecewise[{{x + 10, x < 0}}, None], 
    Piecewise[{{x + 10, x >= 0}}, None]}, {x, -9, 9}, 
   PlotStyle -> {Dotted, Red}];
plot2 = Plot[4 Sin[x], {x, -5, 6}, MeshFunctions -> {#1 &}, 
   Mesh -> {{4}}, MeshShading -> {Directive[Dashed, Blue], Green}];
plot = Show[plot1, plot2, PlotRange -> All];
markers1 = 
 Graphics[{{Dotted, Line[{{0, 0}, {.5, 0}}]}, {Thick, Red, 
    Line[{{.5, 0}, {1, 0}}]}}];
markers2 = 
 Graphics[{{Dashed, Blue, Line[{{0, 0}, {.8, 0}}]}, {Thick, Green, 
    Line[{{.8, 0}, {1, 0}}]}}];

Legended[plot, 
 Placed[SwatchLegend[{Opacity[1], Opacity[1]}, {Function1, Function2},
    LegendMarkers -> {markers1, markers2}, 
   LegendMarkerSize -> {40, 20}, 
   LegendFunction -> (Framed[#, RoundingRadius -> 4, 
       FrameStyle -> Brown] &)], {.2, .8}]]

enter image description here

Original

plot = Plot[{Piecewise[{{x + 10, x < 0}}, None], 
    Piecewise[{{x + 10, x >= 0}}, None]}, {x, -9, 9}, 
   PlotStyle -> {Dotted, Red}];

markers = 
  Graphics[{{Blue, Dotted, Line[{{0, 0}, {.5, 0}}]}, {Thick, Red, 
     Line[{{.5, 0}, {1, 0}}]}}];
Legended[plot, 
 Placed[SwatchLegend[{Opacity[1]}, {Function}, 
   LegendMarkers -> markers, LegendMarkerSize -> {40, 20}, 
   LegendFunction -> (Framed[#, RoundingRadius -> 4, 
       FrameStyle -> Brown] &)], {.2, .8}]]

enter image description here

$\endgroup$
4
  • $\begingroup$ Thanks! This works, but how can I make it work within Epilog's LineLegend? As I mentioned above I'm using Show and Epilog to create a plot with multiple graphs/functions, but it seems that Graphics does not work in Epilog's LineLegend, see screenshot 2 above. $\endgroup$ Mar 12, 2022 at 7:53
  • $\begingroup$ Epilog,Inset,Framed,Show is not the good way to add Legend, we just need to use Legended function to do all this thing. $\endgroup$
    – cvgmt
    Mar 12, 2022 at 7:59
  • $\begingroup$ OK. How can I use Legended to plot multiple functions? I have looked at the documentation but that does not seem to be its intended use. $\endgroup$ Mar 12, 2022 at 8:09
  • $\begingroup$ Thanks for the edits. This works great! $\endgroup$ Mar 13, 2022 at 5:54
4
$\begingroup$
Show[function, Epilog -> Inset[Framed[
    LineLegend[
     {Directive[Black, 
       Dashing[ConstantArray[0.004, 12]~Join ~
         ConstantArray[0.06, 1]]]}, {Text@
       Style["Function", Medium, 18, FontFamily -> "Euclid Fraktur"]}
     , LegendMargins -> 7
     ]
    , RoundingRadius -> 3
    ]
   , {-5, 15.5}, {0, 0}, 50]
 ]

enter image description here

$\endgroup$
8
  • $\begingroup$ Unfortunately this does not work for me, see screenshot 1 attached to the original post. $\endgroup$ Mar 12, 2022 at 7:38
  • $\begingroup$ i.stack.imgur.com/FbcOR.png $\endgroup$
    – Syed
    Mar 12, 2022 at 7:46
  • $\begingroup$ Any ideas on what the issue might be on my end? $\endgroup$ Mar 12, 2022 at 7:47
  • $\begingroup$ Use a font like "Times New Roman" or something that is available on your system. It can't be that but let's rule it out. $\endgroup$
    – Syed
    Mar 12, 2022 at 7:51
  • $\begingroup$ OK, done! Unfortunately this didn't fix the problem, but as you said at least we can rule it out now. $\endgroup$ Mar 12, 2022 at 7:54

Your Answer

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

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