2
$\begingroup$

Below are screenshots of the x-axis labels plots generated and exported in SVG format using exactly the same code, either "interactively" or "programmatically":

interactively

enter image description here

programmatically

enter image description here

By "interactively" I mean that the code was run during an interactive session, in a Mathematica notebook. By "programmatically" I mean that the code was run by a WolframScript script executed on the Unix command line.

Note that the interactive version produces smooth fonts, while the programmatic one produces jagged bit-mapped fonts.

How can I get the script to produce the smooth letters I get in the front end?


If you want to reproduce a similar result, follow these steps:

First, define the following function in the Mathematica front-end

exportChart[file_] := (
   SeedRandom[0];
   Export[file,
    Framed[
     BoxWhiskerChart[
      Table[
       RandomVariate[NormalDistribution[mu, 1], 100], {mu, {0, 3, 2, 5}}]
      , ImageSize -> 75
      , ImageMargins -> 30
      , BarOrigin -> Left
      ]
     , FrameStyle -> Opacity[0]
     ]
    ]);

...and invoke it with a suitable filepath as argument; for example:

exportChart["/tmp/frontend.svg"];

Next, copy the code for exportChart to a text file (e.g. save_svg.wl), and add an invocation of exportChart with a different filepath as argument; for example:

(* file: save_svg.wl *)

exportChart[file_] := (
   SeedRandom[0];
   Export[file,
    Framed[
     BoxWhiskerChart[
      Table[
       RandomVariate[NormalDistribution[mu, 1], 100], {mu, {0, 3, 2, 5}}]
      , ImageSize -> 75
      , ImageMargins -> 30
      , BarOrigin -> Left
      ]
     , FrameStyle -> Opacity[0]
     ]
    ]);

exportChart["/tmp/cli.svg"];

Now run this script using the WolframScript interpreter. In Unix, this would be something like this:

$ WolframScript -script save_svg.wl

Now you should have two *.svg files which should, in principle, be identical. For me at least, they aren't, due to font differences like those shown above.

This is what my /tmp/frontend.svg and /tmp/cli.svg look like.

/tmp/frontend.svg

enter image description here

/tmp/cli.svg

enter image description here


NB: The business with the zero-opacity frame in exportChart is voodoo that somehow avoids a Mathematica bug that occasionally results in malformed SVG exports.

$\endgroup$
  • $\begingroup$ On Windows 10 with 11.0.1 I believe my .svg's look identical, FWIW. Consider imgur.com/a/Dk0DO, left from wolframscript, right generated by evaluating the same code in a notebook. $\endgroup$ – user6014 Mar 14 '17 at 14:01
  • $\begingroup$ @user6014: Thanks for checking! $\endgroup$ – kjo Mar 14 '17 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.