2
$\begingroup$

I have many plots (that take a long time to run) and each is saved to a variable name. For simplicity, say one was:

m = Plot[Sin[x], {x, -1, 1}, ImageSize -> 600]  

If I call for the plot

m

it will be presented as it should be, with ImageSize 600. I can click and drag the corners to change the size of the plot (instantaneously, so I know mathematica is not re-computing the plot, but simply adjusting the output image size continuously).

Therefore, what I wish to accomplish must be possible to do, but I don't know how to instruct mathematica to do it.

What I want is a command, something similar to, say

m /. ImageSize -> 200  

etc. that can present m with a new, prescribed size, rather than manually sliding it to a new size, and certainly without having to re-compute the code that produced the saved plot. Help?

$\endgroup$
5
  • 8
    $\begingroup$ Or just Show[m, ImageSize -> 200] $\endgroup$
    – Öskå
    Commented May 19, 2014 at 15:28
  • 1
    $\begingroup$ Steve, @Öskå is the correct answer. While using the rule method works, it depends on the internal structure of Graphics to do so. Using Show does not. Well, in the sense that you no longer have to worry about it. $\endgroup$
    – rcollyer
    Commented May 19, 2014 at 15:30
  • $\begingroup$ Awesome, thanks, Show should work for me. $\endgroup$
    – Steve
    Commented May 19, 2014 at 15:37
  • $\begingroup$ # /. (ImageSize -> (ImageSize /. Last@#)) -> (ImageSize -> 200) &[m] directly replaces the previously defined size definition in your case, as FullForm will demonstrate you. $\endgroup$
    – UDB
    Commented Jun 1, 2014 at 21:33
  • $\begingroup$ Another way: Image[m, ImageSize -> 200]. $\endgroup$
    – bill s
    Commented Sep 1, 2015 at 15:20

1 Answer 1

3
$\begingroup$

There are two straight-forward ways of doing this. The first, as mentioned in the comments by Oska is

Show[m, ImageSize -> 200]

As mentioned by Guess Who It Is in a comment, this is the more idiomatic way of doing things, as this is exactly what Show is for. To understand what this does, do

Last@Normal@m

and

Last@Normal@Show[m, ImageSize -> 200]

Both have the option ImageSize -> 600 buried in the list of Options, but the second has ImageSize -> 200 prepended to the list. Plot uses the first instance of an option, so the new ImageSize overrides the original.


The second, which uses a replacement rule similar to one in a comment by UDB, is

mNew = m /. HoldPattern[ImageSize -> _] -> (ImageSize -> 200)

This replacement rule find a match for the pattern HoldPattern[ImageSize -> _] and replaces it with the new one. Now, if you do

Last@Normal@mNew

you will now find ImageSize -> 200 in the place where ImageSize -> 600 used to be.

Moral of the second story: it helps to remember that everything in Mathematica is an expression (including graphics), and so parts can be replaced with the relevant replacement rule.

$\endgroup$
1
  • 2
    $\begingroup$ I'd say Show[] is more idiomatic tho; it's precisely the function intended for post hoc changes of graphics settings. $\endgroup$ Commented Sep 1, 2015 at 7:31

Your Answer

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

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