5
$\begingroup$

Is it possible to move the first plot in show to the front without changing which plot we inherit the options from?

The problem is that if I want a certain plot to go in front I cannot use it to inherit the options from.


While writing I thought of the following solution. I can include the plot twice. Once as the first argument and again later. Is this the best solution?

$\endgroup$
5
  • 3
    $\begingroup$ Does it work for you Show[otherPlots.., Graphics[##], ##2]& @@ thePlot ? $\endgroup$
    – Kuba
    Commented Dec 3, 2019 at 13:01
  • 1
    $\begingroup$ I'd try Show[plot1, plot2,..., Options[plot2]], but in both my and @Kuba's suggestions, if there is an option in the first plot that is not explicitly overridden by the options, then the option from the first plot will be used. $\endgroup$
    – Michael E2
    Commented Dec 3, 2019 at 13:45
  • 2
    $\begingroup$ If the result of Show looks the way you want it to, I would say your solution of giving one of the plots as both first and last argument is a good solution even if it it not the best solution. I take an engineering view of things; if a solution works without any unwelcome side effects, use it and don't worry about what is best. $\endgroup$
    – m_goldberg
    Commented Dec 3, 2019 at 13:46
  • $\begingroup$ You can also do Show[plot1, plot2, plot2[[2]]]. If you want to suppress the options from first plot entirely you can do Show[Graphics@First@plot1, plot2, plot2[[2]]]. $\endgroup$
    – kglr
    Commented Jan 7, 2020 at 19:29
  • $\begingroup$ How can the code be generalized to work even when PlotLegends is present? $\endgroup$
    – NC520
    Commented Dec 14, 2022 at 22:59

1 Answer 1

3
$\begingroup$

This is an interesting and common question so, to summarize the suggestions in comments:

  • @Kuba suggested:

    Show[otherPlots.., Graphics[##], ##2]& @@ thePlot
    
  • @Michael pointed out the following alternative:

    Show[plot1, plot2,..., Options[plot2]]
    

    but also mentioned that, if there is an option in the first plot that is not explicitly overridden by the options, then the option from the first plot will still be used.

  • As OP themselves mentioned in their question, a pragmatic approach suggested by @m_goldberg would be to simply have one of the plots both first and last in Show. Although perhaps inefficient, it will work and will not suffer from the stray option problem mentioned above.

$\endgroup$

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.