Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Flat colour in RegionPlot; millions of little triangles

640 views
Skip to first unread message

Will Robertson

unread,
Sep 29, 2007, 2:21:17 AM9/29/07
to
Hello,

I'm creating a bunch RegionPlot figures at the moment and I'm a little
concerned by the algorithm Mathematica uses to save the figure.
Consider something like this, adapted from the RegionPlot help:

RegionPlot[
Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
1.5}]
Export["region-plot.svg", %];

(I've chosen SVG just so I can open the image in Inkscape. The effect
is visible in EPS and PDF images as well.)

The resulting image is a funny shape that consists of millions (I
exaggerate) of polygons that fill the region, obviously corresponding
to the mesh used in Mathematica to create the region. However, it also
creates a grey outline that is a *single* graphic object that is
otherwise transparent.

In Inkscape, I can delete *all* of the polygons and apply a fill
colour to the single graphic object that defines the outline. This
reduces the complexity and file size of the image by orders of
magnitude with the same (or better!) output quality.

Is there a way to replicate this process within Mathematica itself
before I output to PDF? I am loath to perform this task manually in
Inkscape every time I generate an image.

Many thanks,
Will Robertson


Jean-Marc Gulliet

unread,
Sep 29, 2007, 6:25:25 AM9/29/07
to
Will Robertson wrote:

> I'm creating a bunch RegionPlot figures at the moment and I'm a little
> concerned by the algorithm Mathematica uses to save the figure.
> Consider something like this, adapted from the RegionPlot help:
>
> RegionPlot[
> Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
> 1.5}]
> Export["region-plot.svg", %];
>
> (I've chosen SVG just so I can open the image in Inkscape. The effect
> is visible in EPS and PDF images as well.)
>
> The resulting image is a funny shape that consists of millions (I
> exaggerate) of polygons that fill the region, obviously corresponding
> to the mesh used in Mathematica to create the region. However, it also
> creates a grey outline that is a *single* graphic object that is
> otherwise transparent.
>
> In Inkscape, I can delete *all* of the polygons and apply a fill
> colour to the single graphic object that defines the outline. This
> reduces the complexity and file size of the image by orders of
> magnitude with the same (or better!) output quality.

<snip>

Could you be more precise about the size of the exported files (before
and after manual fixing) and about what size you expect and deem as
reasonable? I am asking because when I tried the following code on my
system (Windows XP SP2, Mathematica 6.0.1), the exported file (in PDF
format since I do not have Inkscape) is about 40 KB, size that does not
look that huge. However, it is possible that the resulting file size is
dependent of the platform used (Windows, Linux, Mao).

In[1]:= RegionPlot[


Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
1.5}]

Export["region-plot.pdf", %];

[... graphic deleted ... file is about 40 KB]

In[3]:= $Version

Out[3]= "6.0 for Microsoft Windows (32-bit) (June 19, 2007)"

You can get the file at

http://homepages.nyu.edu/~jmg336/mathematica/region-plot.pdf

Regards,
--
Jean-Marc

Chris Chiasson

unread,
Sep 29, 2007, 6:28:26 AM9/29/07
to

lol...


Will Robertson

unread,
Sep 29, 2007, 6:29:27 AM9/29/07
to

Hi Jean-Marc,

Thanks for the reply.

On 29/09/2007, at 16:19 , Jean-Marc Gulliet wrote:
> Could you be more precise about the size of the exported files

> (before and after manual fixing) and about what size you expect and =

> deem as reasonable?

Sure, the example I posted is not particularly large or complex (but
it does demonstrate the problem). When calculating RegionPlot with a
troublesome function that requires a very tight mesh, however, the
problem is much worse. (Displaying the PDF in Acrobat Reader is
visibly delayed as it stops to draw every single blue polygon.)

I can't test out a more complicated example until I get back to my
office but you might find that this produces a more troublesome PDF:
ContourPlot[Sin[x y],{x,=E2=88=925,5},{y,=E2=88=925,5}]

Hope this helps,
Will


Steve Luttrell

unread,
Sep 29, 2007, 6:37:05 AM9/29/07
to
You can use Mathematica to automatically delete all of the polygons used to
simulate the fill.

This creates the raw graphic.

g = RegionPlot[


Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
1.5}]

This filters the graphic to remove all of the polygons.

g2 = g /. {Polygon[__] -> {}}

If you want to look at the expression that Mathematica uses to represent a
graphic (or anything else) then wrap it in FullForm. Thus FullForm[g] will
show the expression with all of the polygons present, and FullForm[g2] will
show the filtered version of the expression without polygons.

--
Steve Luttrell
West Malvern, UK


"Will Robertson" <wsp...@gmail.com> wrote in message
news:fdkqst$7bk$1...@smc.vnet.net...

AnttiPenttilä

unread,
Sep 30, 2007, 3:55:16 AM9/30/07
to
Hi,

The PDF in the link from Jean-Marc has some white lines between polygons
inside the regionplot. In EPS they are even more visible. I have noticed
the same problem in one plot of mine which was intended to a scientific
publication. The exported EPS plot was terrible because there was a very
dense grid of polygons clearly visible in the plot. I think that this
should be corrected by Wolfram.

Regards,
Antti

c

unread,
Sep 30, 2007, 4:03:29 AM9/30/07
to
This is a great hack, and it works on things like Plot3D (of course).
However, I can't get it to work if one uses the option Mesh -> All in
the original generating expression (everything except the intersection
of the surface with the bounding frame is wiped out) . What am I doing
wrong?


On Sep 29, 4:37 am, "Steve Luttrell"

Will Robertson

unread,
Sep 30, 2007, 4:05:32 AM9/30/07
to
Hi Steve,

Thanks for your reply.

You wrote:
> This filters the graphic to remove all of the polygons.
>
> g2 = g /. {Polygon[__] -> {}}

This looks exactly like what I had in mind, thanks!
When I get back to the office next week I'll give it a shot.
Without looking at it at the moment I'm guessing that I can use a
similar technique to fill the outline of the region (by looking at
FullForm of the graphic, that is).

I'm pretty new to Mathematica in general so your advice to look inside
the graphic is much appreciated and very timely.

All the best,
Will


ulises

unread,
Oct 1, 2007, 4:52:20 AM10/1/07
to

Use:


RegionPlot[
Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,

1.5}, PlotStyle -> None]


Note that the interior polygons are tere for a reason, for example,
this works as expected:

g = RegionPlot[


Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,

1.5}, PlotStyle -> None]

g2 = g /. {Line[x_] -> {Line[x], Polygon[x]}}


so, why we do not do this by default? well because then this will not
work:

RegionPlot[x^2 + y^2 > 1 && x^2 + y^2 < 9, {x, -3, 3}, {y, -3, 3}]


g = RegionPlot[x^2 + y^2 > 1 && x^2 + y^2 < 9, {x, -3, 3}, {y, -3, 3},
PlotStyle -> None]

g2 = g /. {Line[x_] -> {Line[x], Polygon[x]}}


you need an interior mesh in order to handle interior holes in
regions, as well as general Mesh lines and other processing of other
options.

Ulises Cervantes
WRI


AnttiPenttilä

unread,
Oct 2, 2007, 5:20:41 AM10/2/07
to
ulises wrote:
> Use:
>
>
> RegionPlot[
> Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
> 1.5}, PlotStyle -> None]
>
>
> Ulises Cervantes
> WRI
>

..but then there is no color fill in the region. Is there any way that
the color fill is there, but the polygon lines don't show in, e.g. EPS
format exported figures?

Antti


Jean-Marc Gulliet

unread,
Oct 2, 2007, 5:44:06 AM10/2/07
to
Will Robertson wrote:

Hi Will,

In version 6, the quality of a plot depends on the value of some new
options such as *MaxRecursion* (my understanding is that this option
controls the behavior of the new adaptative sampling algorithm) and also
*PlotPoints*.

Below you will find sizes of pdf files created from *ContourPlot* with
various settings of *MaxRecursion* and *PlotPoints*.

Using the default values, we can see that the pdf file is about 1.5 Mb,
whereas, setting *MaxRecursion* to 0, the size is about 72 Kb, i.e 21
times less.

Now, look at the quality (aesthetic) of the graph in both cases: the
differences ares striking and the visual appearance of the former graph
is so poor that it looks like it has not been produced by the same
application (at least to my eyes :-)

You can find the evaluated notebook and a pdf version at

http://homepages.nyu.edu/~jmg336/mathematica/contour-plot.pdf

http://homepages.nyu.edu/~jmg336/mathematica/contour-plot.nb


In[1]:= $Version

Out[1]= "6.0 for Microsoft Windows (32-bit) (June 19, 2007)"

In[2]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
PlotLabel -> "MaxRecursion->Automatic\nPlotPoins->Automatic"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[4]= 1535524

In[5]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 0,
PlotLabel -> "MaxRecursion->0\nPlotPoins->Automatic"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[7]= 72915

In[8]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 1,
PlotLabel -> "MaxRecursion->0\nPlotPoins->Automatic"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[10]= 321697

In[11]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 2,
PlotLabel -> "MaxRecursion->2\nPlotPoins->Automatic"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[13]= 1535522

In[14]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 2, PlotPoints -> 5,
PlotLabel -> "MaxRecursion->2\nPlotPoins->5"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[16]= 214543

In[17]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 2, PlotPoints -> 6,
PlotLabel -> "MaxRecursion->2\nPlotPoins->6"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[19]= 509790

In[20]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 2, PlotPoints -> 7,
PlotLabel -> "MaxRecursion->2\nPlotPoins->7"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[22]= 602774

In[23]:= ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5},
MaxRecursion -> 2, PlotPoints -> 10,
PlotLabel -> "MaxRecursion->2\nPlotPoins->10"]
Export["contour-plot.pdf", %];
ByteCount /. FileInformation[%]

Out[25]= 905323

Regards,
--
Jean-Marc

Will Robertson

unread,
Oct 2, 2007, 6:00:19 AM10/2/07
to
Hi Ulises,

Thanks for your informative reply.

On Oct 1, 5:52 pm, ulises <uli...@wolfram.com> wrote:
> RegionPlot[
> Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
> 1.5}, PlotStyle -> None]

Okay, this gives me an empty shape, which is a good start.
This gives the same result as Steve's suggestion


g2 = g /. {Polygon[__] -> {}}

But now I've got an empty shape that I'd like to fill.

> Note that the interior polygons are tere for a reason, for example,
> this works as expected:
>
> g = RegionPlot[
> Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,
> 1.5}, PlotStyle -> None]
> g2 = g /. {Line[x_] -> {Line[x], Polygon[x]}}

I'm not sure what I should expect here, sorry. When I export this
image it is much larger than the original shaded image.

> you need an interior mesh in order to handle interior holes in
> regions, as well as general Mesh lines and other processing of other
> options.

Okay, that's fair enough.

Let's return to my original problem, which I've almost solved thanks
to everyone here.

g = RegionPlot[
Abs[Nest[(#^2 + x + I y) &, x + I y, 16]] < 2, {x, -2, 1}, {y, -1.5,

1.5}, MaxRecursion -> 5]
Export["large.svg", g];
g2 = g /. {Polygon[__] -> {}};
g3 = g2 /. {Line[x_] -> Polygon[x]}
Export["small.svg", g3];

large.svg is 831kB, small.svg is 62kB. Obviously MaxRecursion
magnifies the difference. This code produces a figure that is much
smaller (complexity and file size) compared to the original, while
printing *exactly the same shape* (viewing the original image in
Acrobat after PDF conversion, for example, is painfully slow). Further
fiddling is required to get the desired fill and edge colours, but I
assume it's not too hard.

Now, the solution above won't work for shapes that have interior
holes. That's too bad, but for some applications, no problem at all.
Presumably it's not impossible to generalise this solution for these
cases, but it's probably non-trivial (hmmm, or is it?). I'd have to
look into the details more, but I don't have time at the moment.

Thanks again for all your help,
Will Robertson


0 new messages