Saving high-quality graphics through commands

Control image size as Graph option:

g = CompleteGraph[100, ImageSize -> 2000];
Export["mysuperawesomegraph.png", g]

Also if you already have graphics produced, you can use Show to programmatically resize it:

g = CompleteGraph[100, GraphStyle -> "LargeNetwork"];
gmag = Show[g, ImageSize -> 2000];
Export["mysuperawesomegraph.png", gmag]

In Mathematica there is a difference between Graphics objects and images:

RandomImage[1, {100, 100}]

enter image description here

In[1]:= % // Head
Out[1]= Image

Graphics[Raster[RandomReal[1, {100, 100}]]]

enter image description here

In[2]:= % // Head
Out[2]= Graphics

ImageResize is used for images and may result in the loss of resolution. Changing shown size of Graphics with Show will not result in loss of resolution.


What I found to be working best is using before the final export. Especially when your adding labels etc you get some problems (at least I do).

Example:

graph = CompleteGraph[100, VertexLabels -> "Name",ImageSize -> Scaled[10]];
Export["graaf.pdf", graph];

when I open the graaf.pdf at 100% and use the snipping tool I get:

enter image description here

Using:

graph2 = CompleteGraph[100, VertexLabels -> "Name"];

Now I have to enlarge the pdf to approx 4000% and I get again with the Snipping tool on the same vertex 80:

enter image description here

With the ImageSize at a certain level like mentioned and then exporting to a PNG file will also work but it takes more to get to the right resolution.


Usually, another way to control the exported image size would be:

Export["graph.png", graph, ImageSize -> 2000]

This doesn't work correctly unless you prepare the graph with a sufficiently thin EdgeStyle. I did that in the example graph that I initially played with, because it looked better in the notebook to begin with.

Here is what I get in the external file from the above export command applied to a graph defined as

graph = CompleteGraph[100, EdgeStyle -> Thickness[.0001], ImageSize -> 360]

ImageSize 2000

and here is what I get when I double the width, ImageSize -> 4000:

ImageSize 4000

There is still something buggy to see here, though: the Thickness of the circles is scaled up, in the same way that the edge thickness becomes too thick if I don't set it to a small value beforehand.

End edit

In fact, I wonder if it would be better for you to export in vector format and not in a rasterized bitmap format. For example, if you can work with PDF files, you get much smaller file sizes with lots of detail. With this graph

graph = CompleteGraph[100, EdgeStyle -> Directive[Thin,Opacity[.2]], ImageSize -> 360]

and the export command

Export["graph.pdf", graph]

you get a 77kB PDF file, whereas the exported PNG file above is almost 7MB large. Note that I didn't include an image size in the last command because it is irrelevant for the amount of detail in the exported vector format.

If you want to publish the exported figure on the web, it may also be useful to export as SVG because most browser now support it. Try this:

Export["graph.svg", graph]

and drag it to Firefox to see the amount of detail in this file.