# Can you plot pure function without specifying variable?

Given a pure function, e.g. f=Sin[#]&, it is possible to plot it with introducing an arbitrary local variable, e.g.,

Plot[f[x],{x,0,1}]


However, the introduction of a variable x seems unnecessary. Is it possible to plot this without specifying a name for the variable? If not, is there a good reason why this functionality doesn't exist?

• AFAIK it only exists for InterpolatingFunction. Apr 13, 2016 at 0:32
• One could similarly ask why Integrate[] needs a dummy variable... Apr 13, 2016 at 0:44
• myPlot[f_, r_] := Plot[f[First@r], r]; myPlot[f, {x, 0, 1}]
– ciao
Apr 13, 2016 at 9:21

Yes, you can plot it, but not using Plot. For example, you could map the function over a range of values and then use ListLinePlot:

With[{xmin = 0, xmax = 4π},
ListLinePlot[f/@Subdivide[##,100],DataRange->{##}]&[xmin,xmax]
]


This uses the new function Subdivide with 100 plot points.

The reason why Plot requires you to specify a dummy variable is that it takes expressions and not functions as its argument. Therefore, the plot variable is not identifiable by a slot, and you need to specify it by naming the plot variable.

• One of course loses out on the adaptive sampling of Plot[] in the process. Apr 13, 2016 at 1:41
• @J.M. Yes - but one person's loss is another person's gain - sometimes the sampling slows plotting down and you end up turning it off anyway...
– Jens
Apr 13, 2016 at 2:16

There is the function Unique, which is often used to avoid symbol name collisions.

You could use it like this:

Plot[f[#], {#, 0, 10}] &[Unique[]]


Of course, in another sense, it's the most arbitrary that a variable can get (it's just a variable with a name of the form $12345 where 12345 represents some arbitrary number). • I can't help but feel that this is a cheat of sorts; you still specified a variable that just happened to be randomly generated. Apr 13, 2016 at 4:19 • @J.M. Sure, but I think it's often what people would want. For example, you might have a the function f[a_] := D[a x^2, x] /. x -> 1 and not have access/be aware to the fact it is dependent on x internally, as it just looks like a simple mapping from a to f[a], but if you plot it with x you get a very different result to any other symbol. Apr 13, 2016 at 14:07 The short answer is no, not with Plot, which like many other Mathematica built-in functions that range over a set of values, is designed to follow a standard syntax pattern for which Table may be considered the prototype. Consider Do or even Manipulate$\text{*}$, which was added long after Plot. Both follow this pattern although in the case of Manipulate the semantics are wildly different.$\text{*}\$ See this conference article for a discussion of how Manipulate was deliberately designed to emulate Table. It also has many additional interesting things to say about Manipulate.

As an additional small note to ciao's comment, it's possible to define your own function based on Plot to work with pure functions, e.g.:

plotPure[fun_, d_, args___] :=
Plot[Evaluate@If[ListQ[fun], Through[fun[x]], fun[x]], {x, d[[1]], d[[2]]}, args];


This way it provides all Plot functionality:

plotPure[{Sin, Sin[#]^3 &}, {-Pi, Pi}, PlotStyle->"Thick"]