开发者

Mathematica: Changing the options of a Graphics object

开发者 https://www.devze.com 2023-03-11 17:22 出处:网络
Is it possible to change the options of a Graphics object? Say you are working with a graphics object G2D as in the following picture

Is it possible to change the options of a Graphics object? Say you are working with a graphics object G2D as in the following picture

Mathematica: Changing the options of a Graphics object

You can see from the InputForm of G2D that the PlotR开发者_开发百科ange option is set to {{-0.025,1.025},{0,1.05}}. But later on on the code I decided to change the PlotRange option to a different one. What happens with the InputForm? The new option simply gets appended.

You can obtain the options set by a graphics object by using Options and AbsoluteOptions but I haven't found a way to change those options. The function SetOptions seemed like a likely candidate but it turns out that this function only works with streams and functions. That is, it only allows to set the default behavior as they show in the examples.


If you want to clean up the set of options in the graphic, it's probably easiest to just construct the graphic anew. You can extract the main body of the graphic with First, and use DeleteDuplicates and Options to get the simplified list of options:

old = Graphics[{Blue, Disk[]}];
old = Show[old, ImageSize -> 1000];
old = Show[old, ImageSize -> 500];
old = Show[old, ImageSize -> 250];
old = Show[old, ImageSize -> 100]

InputForm[old]

new = Graphics[First[old], 
  DeleteDuplicates[Options[old], First[#] === First[#2] &]]

InputForm[new]

I've used Options because the options to Graphics can be, but aren't always, enclosed in a list, and Options will standardize the form.

I'd also like to point out that technically Show is prepending the option values, so the duplicated options aren't really harming anything, although they can make it harder to debug graphics output and slightly increase the size of the file.

Mathematica: Changing the options of a Graphics object


You can also use SetOptions to change the default value for all graphics:

SetOptions[Graphics, Background -> Gray];

Graphics[Disk[]]

Mathematica: Changing the options of a Graphics object

0

精彩评论

暂无评论...
验证码 换一张
取 消