Note that there are some explanatory texts on larger screens.

plurals
  1. POMathematica: Labels and absolute positioning
    text
    copied!<p>How do you position a text outside a plot in mathematica? A quick google search will lead you to</p> <p><a href="http://reference.wolfram.com/mathematica/howto/AddTextOutsideThePlotArea.html" rel="nofollow noreferrer">http://reference.wolfram.com/mathematica/howto/AddTextOutsideThePlotArea.html</a></p> <p>This is not enough since you want to achieve this with code. A simple example of placing text in mathematica is the following:</p> <pre><code> Show[ Plot[x^3, {x, -1, 1}, Frame -&gt; True, ImageSize -&gt; Medium, FrameLabel -&gt; {"x", "y"}, PlotRange -&gt; {{-1, 1}, {-1, 1}} ], Graphics[ Text[Style["A", Bold, 14, Red], {.5, .5}]] ] </code></pre> <p>This places the letter A at the point (.5, .5) relative to the plot. Is there a way of placing text relative to the size of image? Everything is done in the plot coordinates as far as I know. The temporary solution I have is to set the option <code>PlotRangeClipping</code> to <code>False</code> and set the text by giving the right coordinates.</p> <pre><code>Show[ Plot[ x^3, {x, -1, 1}, Frame -&gt; True, ImageSize -&gt; Medium, FrameLabel -&gt; {"x", "y"}, PlotRange -&gt; {{-1, 1}, {-1, 1}} ], Graphics[ Text[ Style["A", Bold, 14, Red], {-1.2, 1} ] ], PlotRangeClipping -&gt; False ] </code></pre> <p><img src="https://i.stack.imgur.com/twaeL.png" alt="currentsolution"></p> <p>A disadvantage of this method is that if we change the range of the plot then we need to recalculate the coordinates of the text in order to keep it where we want it (relative to the whole image). </p> <h2>EDIT:</h2> <p>Try to position the <code>Text</code> A outside the plot.</p> <pre><code>Framed[ Show[ Graphics[ {Orange, Disk[{0, 0}, 3.5]}, Frame -&gt; True, PlotRange -&gt; {{-3, 3}, {-3, 3}}, PlotRangeClipping -&gt; True, FrameLabel -&gt; {"x", "y"} ], Graphics[ Text[ Style["A", Bold, 14], ImageScaled[{.1, .95}] ] ] ] ] </code></pre> <p><img src="https://i.stack.imgur.com/aXq0a.png" alt="enter image description here"></p> <h2>EDIT:</h2> <p>In order to find another solution to this problem I started another post which gave me ideas to overcome a problem that belisarius solution had: Exporting the final figure to pdf was a rasterized version of the figure. Check my other post <a href="https://stackoverflow.com/questions/6303500/mathematica-matlab-like-figure-plot" title="here">here</a> for the solution.</p> <h2>FINAL EDIT?</h2> <p>Since the image links are gone and the link in the previous edit has been modified I decided to update the images and include a modified solution of Simon's <a href="https://mathematica.stackexchange.com/a/2271/877">answer</a>.</p> <p>The idea is to create a mask and include the mask before drawing the labels. In this way we are creating our own <code>plotRangeClipping</code>.</p> <pre><code>mask2D = Graphics[{Gray, Polygon[{ ImageScaled[{-0.5, -0.5}], ImageScaled[{-0.5, 1.5}], ImageScaled[{1.5, 1.5}], ImageScaled[{1.5, -0.5}], ImageScaled[{-0.5, -0.5}], Scaled[{0, 0}], Scaled[{1, 0}], Scaled[{1, 1}], Scaled[{0, 1}], Scaled[{0, 0}], ImageScaled[{-0.5, -0.5}] }] }]; </code></pre> <p>In some cases using <code>ImageScaled</code> of <code>{1,1}</code> is not enough to clip the main image. For this reason I have given more coverage by using <code>1.5</code> and <code>-0.5</code>. Now we can draw the image with label as follows:</p> <pre><code>Framed@Show[ Graphics[ { Orange, Disk[{0, 0}, 3.5] }, Frame -&gt; True, PlotRange -&gt; {{-3, 3}, {-3, 3}}, FrameLabel -&gt; {"x", "y"} ], mask2D, Graphics[ Text[ Style["A", Bold, 14], ImageScaled[{0, 1}], {-1, 1} ] ], Background -&gt; Red ] </code></pre> <p>Here is the desired image:</p> <p><img src="https://i.stack.imgur.com/l1djq.png" alt="enter image description here"></p> <p>Notice that I have changed the background of the image to red. This can easily be modified by changing the <code>Background</code> property and for the mask simply change <code>Gray</code> to whatever color you prefer (White) for instance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload