16
$\begingroup$

Does DisplayFunction->Identity do anything at all in Mathematica 8?

In the third edition of Programming in Mathematica, Roman Maeder gives the following explanation:

The option setting DisplayFunction->Identity causes the graphics functions Plot[], Plot3D[], ParametricPlot[], and so on to generate the graphics in the normal way, but not to render the images. We use it if we want to manipulate the graphics further. Afterwards, we can render the images with Show[graphics,DisplayFunction->$DisplayFunction].

He is referring to the following piece of code:

z = x + I y;
cz = {Re[z], Im[z]};
vlines = Table[ N[cz], {x, -Pi/2, Pi/2, Pi/12} ];
vg = ParametricPlot[ Evaluate[vlines], {y, -1, 1}, DisplayFunction -> Identity ][[1]]

SameQ tells me that even without DisplayFunction->Identity, I will still get the same result for vg. Is Maeder's precaution no longer necessary in Mathematica 8?

$\endgroup$
2
  • $\begingroup$ A use for DisplayFunction in later versions: (71345) $\endgroup$
    – Mr.Wizard
    Commented Feb 16, 2015 at 14:34
  • $\begingroup$ Here is another application of DisplayFunction. $\endgroup$ Commented Jul 25, 2016 at 6:39

4 Answers 4

26
$\begingroup$

This option is not relevant since version 6 of Mathematica.

Before version 6, graphics did not display immediately after evauating the (inert) Graphics[] expression. They could be shown using the Show command (it was a side-effect of Show). (This is the reason why the function which today is used to combine graphics has such an unusual name---Show.)

So building graphics went like this:

g = Graphics[ ... ]

(* the output of this would be formatted simply as the string --Graphics-- *)

Show[g]

(* now the graphics was displayed *)

Show displayed the graphics by evaluating its display function. Plotting functions (such as Plot) called Show automatically.

In version 6, any Graphics object is formatted by the front end as the image it represents (instead of the placeholder --Graphics--). Running the DisplayFunction is no longer needed (at least when using the standard notebook interface). But the mechanism is still in place, and we can try it out:

g = Graphics[Circle[], DisplayFunction -> CreateDialog]

(* this is shown the usual way---remember, the front end formats
   any Graphics expression as the "image"/drawing it represents *)

Show[g]

(* now CreateDialog is evaluated and a window with the graphics pops up *)

With the default version 8 DisplayFunction, which is Identity, Show would have returned the original graphics, as applying Identity to something just returns it as it is.

I hope this explains the purpose of DisplayFunction.


Edit:

There are still a number of display mechanisms that use DisplayFunction. One is <<Version5`Graphics` mentioned by @Mr.Wizard. You can find some others by checking the files in $InstallationDirectory/SystemFiles/Kernel/Packages, and the readme file there. The available options depend on the operating system. On Windows, you can try for example, <<Terminal` and <<JavaGraphics` (try both when running the kernel in command line mode, and don't forget to use Show).

$\endgroup$
1
  • 1
    $\begingroup$ That is a very interesting historical explanation! $\endgroup$ Commented May 17, 2012 at 16:30
16
$\begingroup$

==== EDIT: Applications/Usability - after @Jens comment ====

@Jens is right in his comment that this could be useful - thanks for bringing it up. You can define a function just once and then automate its application via global

SetOptions[Plot, DisplayFunction-> myFunctiom]

Now let see how it works.

===============================================================

In addition to the useful answers given, to figure out how things work, you can do a little experimenting of your own. First produce some usual plot

p=Plot[Sin[4 x]/x, {x, -9, 9}, Filling -> 0]

enter image description here

And check its options which will reveal the default setting for DisplayFunction

Options[p]
{AspectRatio -> 1/GoldenRatio, Axes -> True, 
 AxesOrigin -> {0, 0}, DisplayFunction :> Identity, 
 PlotRange -> {{-9, 9}, {-0.868934, 0.863343}}, 
 PlotRangeClipping -> True, 
 PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}}

Now try experimenting:

Plot[Sin[x], {x, -6.6, 6.6}, DisplayFunction -> MyTestFunction]

enter image description here

So you see it just wraps around. So you can now do things like

DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}, 
 ColorFunction -> "SunsetColors", Frame -> False, 
 DisplayFunction -> (GraphicsGrid[ImagePartition[#, 20]] &)]

enter image description here

Which you of course could do just simply wrapping you custom function around your plot in the first place, unless you want to automate your actions as explained in the beginning of the post.

$\endgroup$
4
  • 4
    $\begingroup$ This could be very useful for defining wrapper functions globally as in SetOptions[Plot, DisplayFunction-> myFunctiom]. $\endgroup$
    – Jens
    Commented May 17, 2012 at 16:45
  • $\begingroup$ @Jens I was just wondering why anyone would use DisplayFunction instead of applying the function directly, and your comment showed up. $\endgroup$ Commented May 17, 2012 at 16:50
  • 1
    $\begingroup$ @Jens Instead, one could simply change the value of $DisplayFunction, which is the actual default value of DisplayFunction. One thing ot pay attention to is that the display function won't get applied when evaluating a Graphics expression. It needs Show or some kind of plotting function. $\endgroup$
    – Szabolcs
    Commented May 18, 2012 at 7:43
  • $\begingroup$ @Szabolcs Good point - I don't have any specific use in mind right now anyway... $\endgroup$
    – Jens
    Commented May 18, 2012 at 14:02
11
$\begingroup$

Graphics rendering was fundamentally changed in Mathematica version 6. Before that graphics output was treated specially, like a Print statement. Now output graphics are treated much the same as any other expression, and output can be suppressed with ; (CompoundExpression).

The documentation states:

Between Versions 5 and 6

The graphics functionality has significantly changed. For compatibility purposes, use <<Version5`Graphics` to restore graphics capabilities from Mathematica 5. To restore the Mathematica 6 graphics capabilities, use <<Version6`Graphics`.

Over 800 completely new built-in objects have been added, some of whose names may conflict with names already being used.

The output form of a Graphics or Graphics3D object is now the rendered graphic rather than an output such as -Graphics-.

$DisplayFunction is now set to Identity, so that no side effect happens by default when evaluating a graphic. To produce a side effect similar to Version 5 output, you can use the Print function to print the graphic.

So in fact DisplayFunction -> Identity is the default.

$\endgroup$
5
  • $\begingroup$ Copying and pasting is faster than writing it up :P $\endgroup$
    – Szabolcs
    Commented May 17, 2012 at 16:16
  • $\begingroup$ @Szabolcs I hardly get the chance to answer any questions these days. I'm trying! $\endgroup$
    – Mr.Wizard
    Commented May 17, 2012 at 16:18
  • 1
    $\begingroup$ Something interesting: did you notice that there's <<Version7`Graphics` as well? I didn't check the details, but I remember that when I looked at the code the front end executes upon connecting to the kernel, I had the impression that these files are (also?) used when it connects to an older version kernel. $\endgroup$
    – Szabolcs
    Commented May 17, 2012 at 16:23
  • 1
    $\begingroup$ @Mr.Wizard The link to the list of changes will come in very useful as I read the book. Thank you. $\endgroup$ Commented May 17, 2012 at 16:27
  • 1
    $\begingroup$ Mr.Wizard, and @MichaelWijaya: Not sure if this is what Mr.Wiz intended, but a quick list of changes (with links to more info) is here wolfram.com/mathematica/quick-revision-history.html $\endgroup$
    – rm -rf
    Commented May 17, 2012 at 17:09
11
$\begingroup$

Actually, I know just one special case where DisplayFunction -> Identity is in fact still needed even with version 8:

As I mention in this answer, assume you have started JavaGraphics in a Kernel-only session to make plots. Now you decide you're done displaying plots and instead want to create only non-displayed plots for export. Then you have to do something like this session:

In[1]:= <<JavaGraphics`
 -- Java graphics initialized -- 
(* Make interactive plots... *)
In[3]:= p=Plot[x,{x,0,1}]  
Out[3]= -Graphics-
(* after some more plotting, I want to stop displaying and export only: *)
In[6]:= r=Plot[x,{x,0,1},DisplayFunction->Identity];
In[7]:= Export["r.pdf",r]
Out[7]= r.pdf

The last plot isn't displayed but can be exported. Note that the semicolon ; does not determine whether a plot is displayed or not in this Kernel session. I.e., I could add the semicolon to line In[3] but it would make no difference.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.