Saving high-quality graphics through commands

Say I make and display a graph in a Mathematica notebook:

graph = CompleteGraph[100]


If I drag the corner of the graph to increase its size, and then save it to a file, I get a really big detailed picture of the graph. This is great! I would like to do this without the dragging interaction. Specifically, I want to generate really big graphs and save very detailed images of them by script.

I tried using ImageResize on the graph:

Export["mysuperawesomegraph.png", ImageResize[graph]];


But it seems that the graph object is converted to an image (graphics object) which is scaled up and then saved - so the level of precision in the resulting image is no greater than if one were to call:

Export["mysuperawesomegraph.png", graph];


and manually scale up the image outside Mathematica.

-

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}]


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

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


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:

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:

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]


and here is what I get when I double the width, 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.

-
Your 1st line of code did not work for me. Somehow Export did not respect ImageSize option to produce 2000px image. Did it work for you? My sistem is win7 M.8.0.4. – Vitaliy Kaurov Apr 7 '12 at 4:54
@Vitaly Sure, it works for me on Mac OS X, and it has always worked as far as I know. Maybe you should file a bug report... – Jens Apr 7 '12 at 6:46
It works for other types of graphics on my machine, but for Graph it produces an image where edges cannot be resolved - just uniform color disk. This is why I did not mention this variant in my solution. Format matters - .pdf works. I wonder what other people see on their systems. – Vitaliy Kaurov Apr 7 '12 at 7:52
@Vitaliy Ah, I just looked back at this and did indeed find that something doesn't work right. With the original graph, my first line indeed gives a PNG where the edges are so thick that they fill the area. I hadn't noticed that because I was using EdgeStyle -> Directive[Thin,Opacity[.2]] (which I added to make it look nicer in the notebook). So you're right. – Jens Apr 7 '12 at 16:14