How can i write a function that i can call like this?
(* will have some postprocessing of graphics created by plot later... *)
MyPlotWrapper[Plot[Sin[x], {x,0,Pi}]]
So with each MyPlotWrapper call i want to be able to specify the function that is used to plot (Plot, Plot3D…) and also the function that should be plotted (including all the additional parameters to specify range, lengends… which may be of arbitrary number).
There are many ways, but without knowing what you’re trying to do, it’s difficult to give complete and precise guidance. Here is just one example that will hopefully get you started.
SetAttributes[MyPlotWrapper, HoldFirst];
MyPlotWrapper[Plot[fns_List, range_, options___]] :=
ToString[
StringForm[
"I will plot `` over `` with `` options",
Head /@ fns, Rest@range, Length[{options}]]]
Demonstration:
MyPlotWrapper[Plot[{Sin[x], Cos[x]}, {x, 0, Pi}, PlotLegends -> "Expressions"]]
(* "I will plot {Sin, Cos} over {0, Pi} with 1 options" *)
Thank you for the answer. I hope that now it is clear how to get the arguments that were supplied to Plot.
But how can i get the (head of) function passed as argument? Using Head[Plot] and adding an underscore like this MyPlotWrapper[Plot_[fns_List, range_, options___]] does not seem to work
Is there any possibility to use the pattern p_ and interpret p as a function head and call that function whatever its name name may be (passing it the arguments that were supplied for p)? There are more then two Plot functions and at the end i may also want to call other (non plotting) functions this way. Because of that i would prefer to avoid a switch with a limited number of cases. p_ may be any function that may be passed as argument to Export[] to create an image of it.
Sure! But it would really help to know what you want to do with these plot expressions in your function. Each plot function could have different options or different argument patterns, so if you’re inspecting the expression inside your function, you probably can’t avoid some special-casing.
Anyway, the function MyPlotWrapper that I showed you has “knowledge” of all the parts of the plot expression, so you can just assemble the pieces together when you’re ready to execute the plot. Or, you can just name the whole argument pattern, and avoid having to reassemble the pieces.
Thanks, this helped get me started. Even when i have a valid personal Mathematica license i also use the free Wolfram engine and wanted to get limited Plot support without requiring to fiddle with Jupyter or other alternatives of similar complexity and size.
At the moment i have this which is not finished yet but a good point to start:
(do not evaluate the first argument of ExpGraphShow)
SetAttributes[ExpGraphShow, HoldFirst];
(*export graphics to temp. file and open the GIF image in MS paint
the parameter can be a call to some Plot function
ExpGraphShow2[Plot[{Sin[x],Cos[x]},{x,0,2Pi},PlotLegends->“Expressions”]] )
ExpGraphShow2[head_[func_, rng_, opt___]] :=
Module[{},
(concat directory path for GIF files)
t = $HomeDirectory <> “\Documents\Mathe\Mathematica\Tmp\”;
(get date string in custom format)
d = DateString[{“Year”, “Month”, “Day”, “Hour”, “Minute”, “Second”,“Millisecond”}];
(concat full path of GIF file)
f = t <> d <> “.gif”;
(without FileNames[] the files in t directory are not deleted)
DeleteFile[FileNames[t <> ".gif"]];
(export graphics to GIF file)
Export[f, head[func, rng, opt]];
(*run MS paint to open the GIF file *)
Run[“start /b mspaint”, f]];