3
$\begingroup$

I can a self-defined function for plotting. There is a built-in option AspectRatio. I want to include a few more choices for convenience, eg.

ratio1 = (yrange[[2]]-yrange[[1]])/(xrange[[2]]-xrange[[1]])
ratio2 = 2*(yrange[[2]]-yrange[[1]])/(xrange[[2]]-xrange[[1]])
ratio3 = (yrange[[2]]-yrange[[1]])/(xrange[[2]]-xrange[[1]])/3

other than the standard choices of Automatic and Full. How to do that so that AspectRatio->ratio1 becomes a valid input?

$\endgroup$
4
  • $\begingroup$ in your case it will be 1, 2 and 1/3 right? $\endgroup$
    – Kuba
    Commented Sep 10, 2016 at 9:36
  • $\begingroup$ Can you be more specific on what exactly is not working? AspectRatio->ratio1 works just fine for me, given some arbitrary ranges. $\endgroup$
    – Andreas
    Commented Sep 10, 2016 at 9:50
  • $\begingroup$ the three ratios are just examples (for course I can just type 1, 2 1/3). I mean I have already calculated a few more complicated ratios that will depend on a few parameters of the input function. I don't want to explicitly calculate them every time I call my self-defined plotting function, and so I want to include them in the optional arguments 'AspectRatio' so that when I just type in 'ratio1' it will automatically calculates that particular ratio inside the function itself. $\endgroup$
    – Physicist
    Commented Sep 10, 2016 at 10:02
  • $\begingroup$ In short, I want to put the above codes inside the function, and I can just choose which ratio I want by specifying the argument of 'AspectRatio', but currently it only accepts the built-in Automatic' and Full`, something I want to extend. $\endgroup$
    – Physicist
    Commented Sep 10, 2016 at 10:03

1 Answer 1

2
$\begingroup$

There are probably a lot of ways to do this, I will just demonstrate a simple, straight forward ways with which you might experiment.

First we set up a global variable that assigns aspect ratio functions:

$aspectRatioFunctions = Function[ func,
    Switch[ func,
        "ratio2", Function[ {p1,p2}, 2 p1/p2 ],
        "ratio3", Function[ {p1,p2}, p1 / (3 p2) ],
        _ , Function[ {p1, p2}, p1/p2 ] (* ratio1 as Default *)

    ]
];

We can now use this in a simple graphics function or whatever custom function you want to build upon:

Options[ myGraphics ] = {
    "AspectRatioFunction" -> "ratio1"
};

myGraphics[ graphics_List, aspectPars_ , 
    opts : OptionsPattern[{ myGraphics, Graphics }] ] := With[
    {
        aspectRatio = Apply[
            $aspectRatioFunctions@OptionValue["AspectRatioFunction"],
            aspectPars
        ]
   },
   Graphics[
       graphics,
       Evaluate@FilterRules[ {opts}, Options[Graphics] ], (* more robust *)
       AspectRatio -> aspectRatio
   ]
]

gr = { Red, Line[{{1, 1}, {2, 2}}] }

myGraphics[ gr, { 1, 1 }, AspectRatioFunction -> "ratio2", Imagesize -> Tiny ]

Ratio2

$\endgroup$
3
  • $\begingroup$ This is merely a toy example to demonstrate the principle, of course. $\endgroup$
    – gwr
    Commented Sep 10, 2016 at 13:02
  • $\begingroup$ Note, that using FilterRules makes this more robust; the Evaluate is not needed here but may be for functions like Plot with HoldAll and the like. $\endgroup$
    – gwr
    Commented Sep 10, 2016 at 13:19
  • $\begingroup$ @Physicist Would your maybe care to accept? Thank you. ;-) $\endgroup$
    – gwr
    Commented Sep 20, 2016 at 9:13

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.