Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose that I make a plot of $x^2$ vs. $x$:

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True]

myPlot1

If I click on the plot, I see the orange handles with which I can resize the plot:

myPlot2

Now, suppose that I want to actually crop the plot -- I would like to effectively shrink the orange box so that some of the plot is actually cropped. (Why in the world would I want to do this? It's a long story...)

I have found, from answers to a question that I previously asked, that this is possible using ImagePad with negative arguments. For example, suppose that I want to crop 40 pixels off the right side of the plot. I can do the following:

ImagePad[myPlot, {{0, -45}, {0, 0}}]

myPlot3

It looks good so far. However, when I resize the resulting cropped plot by dragging the orange handles, it looks like the image has been rasterized:

myPlot4

But, in contrast, I need the image to remain a vector image (non-rasterized) after cropping. Is that possible? I am running both version 8 and 9.

share|improve this question
    
Just using PlotRange is no option? Like: Plot[x^2, {x, -1, 1}, Frame -> True, PlotRange -> {{-1, 0.5}, Automatic}] –  Pinguin Dirk Mar 8 at 16:50
    
Or after the fact: Show[plot, PlotRange -> ...] –  Szabolcs Mar 8 at 16:50
    
@PinguinDirk Unfortunately, no, just PlotRange is not an option for me. I need to be able to crop the entire image -- including white space, frames, frame ticks, and frame labels. –  Andrew Mar 8 at 16:54
    
@Andrew I'm not sure I understand. Why can't you just not use frames and labels then? Show[... Frame -> False] –  Szabolcs Mar 8 at 16:58
1  
@Andrew LevelScheme doesn't use Mathematica's special built-in frame. It draws its own frame, so setting PlotRange on the output should in fact crop the frame as well. –  Szabolcs Mar 8 at 17:06

2 Answers 2

up vote 3 down vote accepted

You can use Inset (this is what the interactive editor does basically).

Example: The variable crop consists of image scaled coordinates in the order

{{xmin, xmax}, {ymin, ymax}}

In this example, the value ymax is greater than one, which extends the image beyond the boundary of the original plot.

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True];
croppedPlot =
 With[{iSize = 350. {1, 1/GoldenRatio},
       crop = {{0.1, 0.8}, {0.2, 1.2}}}, 
   Graphics[
    Inset[Show[myPlot, ImageSize -> iSize], {0., 0.}, ImageScaled[{0., 0.}]],
    PlotRange -> crop iSize, ImageSize -> crop iSize]
   ];
Framed[croppedPlot, FrameMargins -> 0, FrameStyle -> Red]

Mathematica graphics

share|improve this answer

I just realized from this Wolfram page that one can crop the "orange box" by Ctrl+dragging one of the orange handles.

myPlot = Plot[x^2, {x, -1, 1}, Frame -> True]

original

and obtain

new

This seems to work pretty well for the question that I asked. A programmatic method would be better, but this use of the front end is OK.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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