29
$\begingroup$

The command

Plot[x^2, {x, -3, 3}, PlotStyle -> Arrowheads[{-.025, .025}]] /.Line -> Arrow

produces this output.

enter image description here

What I don't understand is what Line has to do with anything. It must be the case that the Plot command produces Line objects that can be replaced with Arrow. I haven't seen anything in the documentation for Plot that suggests this is the case.

Without /.Line->Arrow the plot won't have arrowheads at the ends of the graph of the function.

$\endgroup$
7
  • 6
    $\begingroup$ According to the documentation for Plot, Plot normally returns Graphics[{Line[...],...}]. See the last two bullet points under "Details and Options." $\endgroup$
    – Michael E2
    Commented Jul 22, 2014 at 5:28
  • $\begingroup$ I missed that. Thank you for pointing that out. Though it does say, "normally returns...". I hope I don't encounter an abnormal case! Looking at the documentation for Line I think I understand the workings of the /.Line->Arrow. $\endgroup$
    – user246
    Commented Jul 22, 2014 at 5:50
  • 3
    $\begingroup$ Related: "How does Plot work?" $\endgroup$ Commented Jul 22, 2014 at 7:54
  • 1
    $\begingroup$ @MichaelE2 I think that bullet point is missing now, unless I'm overlooking it. $\endgroup$
    – Greg Hurst
    Commented Nov 23, 2021 at 23:51
  • 1
    $\begingroup$ @ChipHurst It's the last bullet point in this documentation for Plot. $\endgroup$
    – Michael E2
    Commented Nov 24, 2021 at 3:22

2 Answers 2

42
$\begingroup$

I can add to Mr.Wizard's answer that when InputForm is wrapped by any head like List (// InputForm // List) or by SequenceForm the output is much more readable because in this case it is represented in StandardForm instead of pure textual representation (and still avoids the evaluation leaks of StandardForm!). StandardForm allows semantic selection by double-clicking, wraps the code by window width, highlights the brackets etc. From the other hand it is worth to know that the width of the usual InputForm output can also be controlled.

For inspecting the low-level structure of graphics I find handy my shortInputForm function originally published here. Since that time it undergoes periodic updates for compatibility with newer Mathematica versions, so I finally decided to put it in GitHub repository. You can load the function directly as follows:

Import["http://raw.github.com/AlexeyPopkov/shortInputForm/master/shortInputForm.m"]

The function should work in Mathematica starting at least from version 8.

  • 01/11/2022 Update: ShortInputForm is finally integrated into the Wolfram Language by including it in the Wolfram Function Repository. Users of Mathematica 12 or higher can install ShortInputForm persistently so that it can be used like a built-in function:

    ResourceFunction["PersistResourceFunction"]["ShortInputForm"]
    

Here is how it formats the output:

Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}] // shortInputForm

screenshot

shortInputForm not just displays a shortened and formatted version of InputForm but also allows to select and copy parts of the shortened code into new input cell and use this code as it would be the full code without abbreviations:

screenshot


An advanced description of the Mathematica graphical programming language can be found in these threads:

Some very usefull tricks for obtaining graphical directives applied to every object on the plot can be found in:

$\endgroup$
10
  • $\begingroup$ I should have been using this function. Good stuff! +1 $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 8:25
  • 3
    $\begingroup$ Regarding the // InputForm // List thing, that's a nice trick, but you can also select the graphic and Convert to InputForm (Ctrl+Shift+I) for an editable InputForm. $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 8:33
  • 2
    $\begingroup$ Good alternative. Convert to InputForm gives the fixed-width output (what may be desired in some cases) while // InputForm // List gives window-width wrapping. $\endgroup$ Commented Jul 22, 2014 at 8:38
  • 2
    $\begingroup$ @MichaelE2 Yes, it would shorten the code for version 10 but I'm trying to keep the function working at least starting from version 8. $\endgroup$ Commented Jul 18, 2015 at 14:56
  • 1
    $\begingroup$ @JasonB. If you will specify me as the author, I agree. $\endgroup$ Commented Aug 27, 2020 at 4:34
14
$\begingroup$

As far as I know the specific output format of Plot (and similar commands) is not documented. I believe it has changed between versions therefore any post-processing (such as your replacement rule) must be considered potentially version dependent.

As Michael comments above the documentation does state:

Plot normally returns Graphics[{Line[...],...}].

With a Filling specification given, Plot returns Graphics[{GraphicsComplex[...], ...}].

This tells you the basic format and informs you that GraphicsComplex may be used, therefore you should at least account for that in your post-processing.

Typically the best way to learn the output form of such functions is to view it yourself using InputForm. You can use Short or Shallow to limit the size of the expression returned:

Shallow[
 Plot[{x, 2 x}, {x, 1, 5}] // InputForm,
 {100, 10}
]

You can also replace parts you don't want to view, e.g. Line objects:

Plot[{x, 2 x}, {x, 1, 5}] /. _Line :> "Line[]" // InputForm

Everything in the output expression can be manipulated, including Graphics primitives and options. See:

Some aspects of Plot are not obvious or intuitive such as details of the styling chosen. See:

And as linked from my answer there:


Plot with arrows

You asked specifically about adding arrowheads to plot lines. Although post-processing is the only approach I know for this, meaning I am not aware of an option for Plot itself, I think it is a good method and I have used it many times.

The syntax for Line and Arrow is similar, and both work with GraphicsComplex, therefore the replacement should be reliable. However to make the replacement more robust you should focus it only on the part of the Graphics object that will contain Plot-generated Line expressions, since for example a Prolog or Epilog might also contain Line expressions. Here with MapAt:

MapAt[# /. Line -> Arrow &, Plot[{x, 2 x, 3 x}, {x, 1, 7}], 1]

Or if you have version 10, the operator form:

arrows = MapAt[# /. Line -> Arrow &, 1];

Plot[{x, 2 x, 3 x}, {x, 1, 7}] // arrows

Worth noting is that even though (AFAIK) you cannot generate Arrow expression from within Plot you can still provide style directives that will take affect after the replacement:

Plot[{x, 2 x, 3 x}, {x, 1, 7},
 PlotStyle -> Array[Arrowheads[#/20] &, 3]
] // arrows

enter image description here

$\endgroup$
2
  • $\begingroup$ My goal is to have arrow heads at the ends of plots of functions. Is there a way to do this without resorting to /.Line->Arrow and potentially having things break in future versions? I have custom plot functions that I use make things look the way I want and I use /.Line->Arrow. $\endgroup$
    – user246
    Commented Jul 22, 2014 at 12:16
  • 1
    $\begingroup$ @YequalsX Please see the update. $\endgroup$
    – Mr.Wizard
    Commented Jul 22, 2014 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.