# How to use Show in case one of the graphics doesn't exist?

I tried to combine three and more graphics with the Show command; but sometimes there is no data, so some of the graphics are empty lists. I tried this:

Show[ grPacing, grAcq, If[grNewMap !={}, grNewMap] ... ]


But there are a lot of errors.

Any ideas?

-

Show has no problem with empty lists:

p1 = Plot[Sin[x], {x, -2, 2}];
p2 = Graphics[{Red, Circle[]}];

Show[p1, p2, {}]


-

The problem you are seing is that If[False,x] returns Null, so you end up with Show[plot1,plot2,Null]

Here's a way around that:

grPacing = Plot[x, {x, 0, 1}];
grAcq = Plot[x^2, {x, 0, 1}];
grNewMap = {};
Show[grPacing, grAcq,If[grNewMap =!= {}, grNewMap, Unevaluated[Sequence[]]]]


You could also use a wrapper that removes everything that's not a Graphics or an option:

ShowValidOnly[a___] := Show@@Cases[{a}, (_Graphics | _Rule | _RuleDelayed)]
ShowValidOnly[grPacing, grAcq, grNewMap]

-
Is there a reason one would not simply use Show[p1, p2, {}]? What am I missing? Also, you could replace Unevaluated[Sequence[]] with ##&[]. – Mr.Wizard Apr 17 '13 at 9:46
I understand.Thank you all! – Liad Apr 17 '13 at 10:08
@Mr.Wizard That's great for cases where your plot function will return either a valid graphics or {}, but in the general case it's nice to be able to perform an arbitrary check. Though you could replace Unevaluated[Sequence[]] with {}. That's just my personal preference. – jVincent Apr 17 '13 at 10:13

Another approach is to use DeleteCases. In this example, where you wish to remove Null, you could use

 Show[  ...   DeleteCases[ graphicStuff , Null]   ... ]


and then Show will be happy.

-