1
$\begingroup$

I would like to export BoxWhiskerChart figure as PDF file. Adding the option ChartElementFunction -> "GlassBoxWhisker" to BoxWhiskerChart increases file size enormously. Here is my MWE.

SeedRandom@2;
data = Table[
   RandomVariate[NormalDistribution[\[Mu], 1], 
    100], {\[Mu], {0, 3, 2, 5}}, {2}];
fig1 = BoxWhiskerChart[data];
fig2 = BoxWhiskerChart[data, ChartElementFunction -> "GlassBoxWhisker"];
Export["fig1.pdf", fig1];
Export["fig2.pdf", fig2];

enter image description here Size of fig1 (top one) is 15KB whereas fig2 (bottom one) has ~1.5MB. How to avoid this issue since I have several BoxWhiskerChart in the same figure and result ~5MB per figure. I would like to export fig2 as PDF with as lowest as possible file size with high quality. Any suggestion?

$\endgroup$
1
$\begingroup$

With rasterizeBackground from this answer you can easily get high-quality PDF file with small filesize and vector axes. The only difficulty is that lines become too thin. To overcome this one can apply a little of preprocessing:

fig2fixed = fig2 /. {col__, l_Line} :> {Thickness[.003], col, l}

Export["fig2.pdf", rasterizeBackground[fig2fixed, 3500]];
SystemOpen[%]; FileSize[%]
Quantity[74.013, "Kilobytes"]

The resulting 74 Kb PDF file look on the screen almost exactly as the original vector file of size 1.45 Mb (the only difference is that it is rendered a bit blurry):

screenshot

The preprocessing pattern needed in this case I've found by looking at the shortened InputForm of fig2 using my shortInputForm function (I've hignlighted the corresponding expression by selecting it in the Notebook):

fig2 // shortInputForm 

screenshot

$\endgroup$
  • $\begingroup$ Thanks @Alexey Popkov, this is acceptable answer. It worked like charm in my case. It reduced the size from ~5MB to ~700KB!! $\endgroup$ – OkkesDulgerci Nov 4 at 16:59
0
$\begingroup$
Export["fig2.pdf", fig2, 
  "AllowRasterization" -> True,
  ImageSize -> 1080
  ];
FileSize["fig2.pdf"]

Quantity[46.538, "Kilobytes"]

Of course, the exported graphics is not a pure vector graphics anymore and thus cannot be scales arbritrarily. That is the price you have to pay for color gradients in your image. But for crisp pictures with more export control, I would suggest to use the (potentially lossless_ png format.

You might also consider to export the pieces with color gradients without the axes and the axes seperately and overlay them afterwards. Then at least the axes with their labeling are still vector graphics.

$\endgroup$

Your Answer

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

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