Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I found that the detailed contour pattern will be altered if we Rasterize the original graphics using high RasterSize or ImageResolution. Here is an example:

g = ContourPlot[Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
       Contours -> Function[{min, max}, Range[min, max, 0.01]],
       ContourStyle -> Directive[Black, Opacity[.3], Thickness[.002]],
       ImageSize -> 500]

which looks like

enter image description here

Now we Rasterize g using four times its original pixel size

dim = ImageDimensions@Rasterize[g];
Rasterize[g, RasterSize -> 4*dim]

and we obtain

enter image description here

So the patterns of the contour lines are different. I firstly think that this problem can be solved by increasing the Thinckness value in ContourStyle, but this will not make the result better. I wonder if we can improve it?


As a workaround, can we try to preserve the pattern by keeping the contour in vectot format (together with axis and ticks), and only Rasterize the background? In this case I think there might be problems with the thickness of contour lines.

share|improve this question
@rm -rf, As how I understand the article, the triangle moire pattern in my example is caused by representing a contour line using more pixels, and the intersections prevails the original pattern. I still do not understand why such intersections can happen since the pixels should always be aligned... –  saturasl Dec 4 '13 at 23:56
add comment

2 Answers

up vote 3 down vote accepted

The key point of Moiré pattern is the ratio of the "pixel" size of your underlying raster grid and the distance of your contour lines. So if you want to get a similar pattern on a finer grid, you need to increase your Contours to insure an invariant ratio.

g = ContourPlot[
   Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
   PlotPoints -> 10,
   ContourShading -> None,
   Contours -> Function[{min, max}, Range[min, max, 0.03]],
   ContourStyle -> Directive[GrayLevel[.7], Thickness[0]],
   ImageSize -> 200];
rg = Rasterize[g, RasterSize -> 200]

moire pattern 1

Block[{magnif = 2.5},
 g2 = ContourPlot[
   Sin[x] Sin[y], {x, -3, 3}, {y, -3, 3},
   PlotPoints -> Round[10 magnif],
   ContourShading -> None,
   Contours -> Function[{min, max}, Range[min, max, 0.03/magnif]],
   ContourStyle -> Directive[GrayLevel[.7], Thickness[0]],
   ImageSize -> Round[200 magnif]];
 dim = ImageDimensions[rg];
  RasterSize -> Round[magnif dim],
  ImageSize -> Round[200 magnif]]

moire pattern 2

share|improve this answer
This works, thank you for pointing out the key reason! –  saturasl Dec 8 '13 at 0:55
@saturasl You are welcome! –  Silvia Dec 8 '13 at 1:00
add comment

The lines on your plot go too close to each other and as a result one pixel on the screen should contain more than one line. Apart of image size, the resulting appearance in such cases is also hugely affected by antialiasing which is True by default:

Rasterize[Style[g, Antialiasing -> False], "Image", RasterSize -> 4*dim]



Answering the "Update" section in the question. If you can work with vector formats you do not need to rasterize anything, just Export as PDF. You can be confused by huge size of the generated file and by little artifacts at the boundaries of the polygons. Both problems can be solved using the FixPolygons package by Will Robertson. It takes some time to merge adjacent polygons but the result renders nicely. Here is how it looks in Acrobat 11 after Exporting from Mathematica 8.0.4 (the colors are wrong, Mathematica 9 probably will generate a file with better colors because it embeds colorspace information):


For producing such PDF file you need to copy the FixPolygons.m file to the "Mathematica/Applications" directory in your user profile and evaluate the following:

<< FixPolygons`
g2 = g // FixPolygons
Export["test.pdf", g2]
share|improve this answer
Thank you for the explanation, the problem is clear now. Your suggestion makes a good start, and the result is improved, but there is still obvious difference... –  saturasl Dec 5 '13 at 19:58
I decide to accept Silvia's answer because it directly solves the original problem. But I also want to express my appreciation for your answer, it will be useful in future! –  saturasl Dec 8 '13 at 1:03
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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