0
$\begingroup$

I want to combine two graphs using Show. It sounds pretty straightforward but it actually does not meet my requirements in the way below..

I have two graphs plot1 and plot2 which use Block.

In plot1, all plots should be created with straight lines.

In plot2 all plots should be created with dashed lines.

I therefore can understand and distinguish them once I look at the combination produced by Show.

My actual code is long so I just paste a simple representer example here:

heightSmall[t_]:= M*t/(g+t)

plot1 = Block[{M = {0, 0.1, 0.2, 0.3}, g = {0, 0.1, 0.2, 0.3}}, 
  Plot[Evaluate[heightSmall[t]], {t, 0, 0.1}, 
   AxesOrigin -> {0, 0}, PlotStyle -> {Red, Green, Blue, Black}, 
   PlotLegends -> {"M, g = 0 Small", "M, g = 0.1 Small", "M, g = 0.2 Small", 
     "M, g = 0.3 Small"}]]

heightAsymptotic[t_]:=M*t/(g+0.001*t)

plot2 = Block[{M = {0, 0.1, 0.2, 0.3}, g = {0, 0.1, 0.2, 0.3}}, 
      Plot[Evaluate[heightAsymptotic[t]], {t, 0, 0.1}, 
       AxesOrigin -> {0, 0}, PlotStyle -> {Red, Green, Blue, Black}, 
       PlotLegends -> {"M, g = 0 Asymptotic", "M, g = 0.1 Asymptotic", "M, g = 0.2 Asymptotic", "M, g = 0.3 Asymptotic"}]]

Show[plot1, plot2]

1) How can I put legends inside each plot and eventually Show (In Show there should be just one legend block stating straight lines and dashed lines for respective plots).

2) In the final plot produced by Show each colour should represent relative heightSmall[t] and heightAsymptotic[t] with straight and dashed lines respectively. I tried but it did not work properly.

3) I do not understand why OverHat[g] in PlotLegends does not produce OverHat of g. I might need to post another question about this.

I do not know is it doable or not. Hopefully, it is.

$\endgroup$
  • $\begingroup$ You can get dashed lines with PlotStyle -> ({Dashed, #} & /@ {Red, Green, Blue, Black}) $\endgroup$ – mikado Oct 16 '16 at 16:28
  • $\begingroup$ I can add an overhat inside a string with the key sequence g, Ctrl+7, ^. This shows up in the legend $\endgroup$ – mikado Oct 16 '16 at 16:36
  • $\begingroup$ Great! Now my two problems have already solved. Thanks @mikado. How about the problem in 1) ? $\endgroup$ – Meva Oct 16 '16 at 16:43
  • $\begingroup$ You can produce both sets of curves in a single plot. For example, using Evaluate[{heightSmall[t], heightAsymptotic[t]}]. This should let you specify a single list of legend strings. $\endgroup$ – mikado Oct 16 '16 at 16:45
  • $\begingroup$ But when I evaluate both, I get: Plot::nonopt: Options expected (instead of {t,0,0.1}) beyond position 2 in Plot[{0.24 +0.16 t^2,0.24 +0.113284 t^2,0.24 -0.198874 t^2,0.24 -4.53683 t^2},<<4>>,PlotLegends->{M, Overscript[g, ^] = 0,M, Overscript[g, ^] = 0.1,M, Overscript[g, ^] = 1,M, Overscript[g, ^] = 10}]. An option must be a rule or a list of rules. all the data it uses is fine as far as I see. $\endgroup$ – Meva Oct 16 '16 at 16:55
1
$\begingroup$

I think the following illustrates most of the features you request

Block[{M = {0, 0.1, 0.2, 0.3}, g = {0, 0.1, 0.2, 0.3}}, 
 Plot[Evaluate[{heightSmall[t], heightAsymptotic[t]}], {t, 0, 0.1}, 
  AxesOrigin -> {0, 0}, 
  PlotStyle -> 
   Join[{Red, Green, Blue, 
     Black}, ({Dashed, #} & /@ {Red, Green, Blue, Black})], 
  PlotLegends -> {"M, g = 0 Small", "M, g = 0.1 Small", 
    "M, g = 0.2 Small", "M, g = 0.3 Small", "M, g = 0 Asymptotic", 
    "M, g = 0.1 Asymptotic", "M, g = 0.2 Asymptotic", 
    "M, g = 0.3 Asymptotic"}]]

enter image description here

You can add an overhat inside a string with the key sequence g, Ctrl+7, ^. This shows up in the legend. (I haven't shown this because it makes a mess of the code I've pasted in).

$\endgroup$
  • $\begingroup$ Great @mikado, thanks so much. So is there any way not to repeat the colour in the legend. Or alternatively, is there any way to have two list of legends showing the results from different functions namely heightSmall[] and heightAsymptotic[] ? And another point, the legend is not shown in the output after saving the plot. $\endgroup$ – Meva Oct 16 '16 at 18:49

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.