# Generate code from figure modification obtained through Drawing tools

Sometimes it is useful to use the Drawing tools palette for adding a label, line or arrow to a graph. Unfortunately when remaking the plot the modifications are not present.

Is there a way to transfer (like copy-paste) the modifications or, even better, to generate the code of the them, like MATLAB allows?

The method to retreive the manual modifications are more or less complicated depending of the kind of modifications you have done.

I will describe how to do it for the simplest case : the operator simply add one object with Drawing tools.

It is assumed that the OP wants that the retreived graphics should be at the exact same position as the initial ones.

First a animation showing how the operator make a manually modified graphic and affects it to a variable (named gr01) :

We are going to inspect what is gr01 and what's inside:

Head[gr01]

Graphics


To see what contains gr01 we one can do InputForm[gr01].

The result is a long complicated expression which is hard to understand.

It's better to take the first element of gr01 : gr01[[1]] (the other element are options, see documentation of Graphics).

InputForm[gr01[[1]]] gives :

{{{}, {}, {RGBColor[0.368417, 0.506779, 0.709798], AbsoluteThickness[1.6], Opacity[1.],
Line[{{1.7453292519943294*^-7, 3.0461741978670854*^-14}, {0.16786653520533412, 0.02817917364184368}, {0.34985290613417436, 0.12239705593052741}, {0.5197793485395309, 0.27017057116817916}, {0.6863725086813754, 0.4711072206735648}, {0.8670856790796513, 0.75183757486502}, {1.0357389209544434, 1.0727551123798749}, {1.218512173085667, 1.4847719159579544}, {1.3979521429533783, 1.9542701939879428}, {1.5707961522619713, 2.4674005519610143}}]}},   {EdgeForm[{GrayLevel[0.], Opacity[1.], AbsoluteThickness[1]}], EdgeForm[None], Arrow[{{0.456803410502701, 1.793764733188572}, {0.8335484908870919, 0.708557516931895}}]}, {}}


That's rather tedious to read, though I have deliberatly minimized the number of point to 10 in the code above Plot[ ....,PlotPoints-> 5,MaxRecursion->0].

That's the reason why I suggest to use a nice function shortInputForm[] created by Alexey Popkov (see here). Here is how to load directly the function:

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


Then shortInputForm[gr01[[1]]]gives :

Interpretation :

• Line[{{1.74533*10^-7, 3.04617*10^-14},<<9>>}] means : Line[{{1.74533*10^-7, 3.04617*10^-14} ... "and 9 others couples of values"}] (here is the effect of the function shortInputForm)

• Arrow[...]is the arrow that the operator has added.

• Other elements, EdgeForm,GrayLevel,Opacity,AbsoluteThickness ... are rendering directives attached to Line[] and Arrow[]

One can retrieve either :

• Arrow[...] alone (gr01[[1,2,3]])

• Arrow[...] with the attached directives (gr01[[1,2]]).

If one only want to retreive Arrow[...] alone, it is simpler to use Cases[gr01,Arrow[___],{0,-1}]

(If you want to retrieve the graphic object + the directives, without using the cumbersome indexation, use : Extract[gr01,Drop[#,-1]& /@ Position[gr01,Arrow[___]]])

myManualModification00=Cases[gr01,Arrow[___],{0,-1}];

{Arrow[{{0.456803, 1.79376}, {0.833548, 0.708558}}]}


Finally :

Suppose one wants to add the modifications to the following graphic :

Plot[{x^2,x^3},{x,0,Pi /2},PlotPoints->10,MaxRecursion->0]


Copy-Paste the graphic inside a Show[_,Graphics[myManualModification00]]

One could use also Epilog->, but it turns out that the Arrow is no more selectable/modifiable in the final graphic.

My teacher just solved the question starting from the suggestion FullForm[g] and then developing the method.

First store in g the plot to which we manually added the text.

Then use t = Cases[g, _Inset, All] and finally Plot[g, Epilog->t]

• You surely want Cases, not Case. – b3m2a1 Jan 11 '18 at 20:54
• Great, you made it! – Henrik Schumacher Jan 11 '18 at 21:00
• I think I would use InputForm rather than FullForm, but it's not usually a huge difference. – Michael E2 Jan 12 '18 at 0:49

The resulting Graphics object contains the code. Just store it, e.g. by executing g = % just after the editing.

• And once stored how can I extract only the code about the inserted labels? To be able to applying the to other figures? – Nisba Jan 11 '18 at 20:06
• Ah, that's what you mean. Have a look at FullForm[g], that tells you how the Graphics object is structure and you can copy parts from it. Better try that with a simple Graphics object for the start. – Henrik Schumacher Jan 11 '18 at 20:09
• FullForm[g] gives a very long expression. I recommend to use the function shortInputFrom obtainable here – andre314 Jan 11 '18 at 20:27
• I can't find the function ShortInputForm, is a display style for the current line? – Nisba Jan 11 '18 at 20:33
• @andre You brought up shortInputFrom, you explain it. – Henrik Schumacher Jan 11 '18 at 20:36

It seems that the primitives added via the Drawing tools palette are always placed at the end of the first argument of Graphics. With andre's example the original Graphics object has List of length=1 as the first argument, hence they can be obtained as follows:
myManualModification00 = Rest[gr01[[1]]]

{EdgeForm[{GrayLevel[0.], Opacity[1.], AbsoluteThickness[1]}], EdgeForm[None],

Then one can proceed as he suggests with Show[_,Graphics[myManualModification00]].