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

Re: Venn diagrams?

96 views
Skip to first unread message

DrMajorBob

unread,
Apr 19, 2011, 6:58:05 AM4/19/11
to
This is 700 times faster at my machine:

Clear[area, d, r1, r2, venn]
area[r1_, r2_,
d_] = -(1/2)
Sqrt[(d + r1 - r2) (d - r1 + r2) (-d + r1 + r2) (d + r1 + r2)] +
r1^2 ArcCos[(d^2 + r1^2 - r2^2)/(2 d r1)] +
r2^2 ArcCos[(d^2 - r1^2 + r2^2)/(2 d r2)];
venn[area1_?Positive, area2_?Positive, overlap_?NonNegative] /;
overlap <= Min[area1, area2] :=
Module[{x2, r1 = Sqrt[area1/Pi], r2 = Sqrt[area2/Pi], d},
d = Which[overlap == 0, r1 + r2,
overlap == Min[area1, area2], r1 - r2,
True, Chop[d /. FindRoot[area[r1, r2, d] == overlap, {d, r1}]]];
Graphics[{Red, Circle[{0, 0}, r1], Blue, Circle[{d, 0}, r2]}]]
venn[5, 3, 1]

http://mathworld.wolfram.com/Circle-CircleIntersection.html

Bobby

On Mon, 18 Apr 2011 05:50:23 -0500, Bob Hanlon <han...@cox.net> wrote:

>
> venn[
> area1_?Positive,
> area2_?Positive,
> overlap_?NonNegative ] /;
> overlap <= Min[area1, area2] :=
> Module[{area, m, x2,
> r1 = Sqrt[area1/Pi],
> r2 = Sqrt[area2/Pi]},
> m = Max[r1, r2];
> area[x0_?NumericQ] :=
> NIntegrate[
> Boole[x^2 + y^2 <= r1^2 &&
> (x0 - x)^2 + y^2 <= r2^2],
> {x, -r1, r1}, {y, -m, m}];
> x2 = If[overlap == 0,
> r1 + r2,
> If[overlap == Min[area1, area2],
> r1 - r2,
> Chop[x0 /.
> FindRoot[area[x0] == overlap,
> {x0, r1}]]]];
> Graphics[{
> Red, Circle[{0, 0}, r1],
> Blue, Circle[{x2, 0}, r2]}]]
>
> venn[5, 3, 1]
>
>
> Bob Hanlon
>
> ---- dantimatter <goo...@dantimatter.com> wrote:
>
> =============
>
> Hey Everyone,
> Is there a nice and easy way to make pretty Venn diagrams with
> Mathematica, where the areas of the circles and intersecting regions
> are to scale?
> Cheers
> Dan
>
>
>


--
DrMaj...@yahoo.com

Murray Eisenberg

unread,
Apr 19, 2011, 6:57:22 AM4/19/11
to
The "mathy" way to say it is simply that the areas should be
proportional to the number of objects!

I might add that how to draw Venn diagrams is a delicate issue of
graphics design: how to shade or color the individual circles and how to
shade or color their various intersections. Fairly straightforward a
decision with just two sets (and their intersection) being represented,
but getting more difficult with three and even thornier with four.

As I recall, it's not yet a closed question how to construct a Venn
diagram for larger numbers of sets (where, as always, you want them "in
general position"). Of course with known sizes of the sets, it's a
different matter.

On 4/18/2011 6:50 AM, dantimatter wrote:
> What I meant by 'to scale' is probably best explained with an example:
>
> Imagine that group A contains 40 objects, group B contains 30 objects, and
> the intersection of A and B contains 27 object. In this case I would really like for the circle representing B to be 75% of the size of the circle representing A, and 90% of B to be overlapped by A.
>
> (There is probably a more 'mathy' way to say this, but I'm not sure what that is. Sorry about that.)
>
> Thanks!
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

dantimatter

unread,
Apr 20, 2011, 4:29:25 AM4/20/11
to
Thanks DrMajorBob, Murray, and Bob Hanlon! To the Bobs especially: your math and coding chops are most impressive. :)

Any thoughts on extensions to three sets? At first I had hoped that it would be straight-forward, but after fiddling a bit myself I'm not so sure.

I'm kinda surprised that Mathematica doesn't have this as a built-in function ....

Cheers
dan

DrMajorBob

unread,
Apr 21, 2011, 3:11:04 AM4/21/11
to
Specifying two-way overlaps for three circles requires solving three
pairwise problems along the lines already shown, then using
center-to-center distances to determine a triangle on which the centers
must lie.

That arrangement determines the intersection of all three circles (if
any), which is not true for real-world counting problems.

Hence proportional diagrams, in general, cannot be limited to circles.

Bobby

On Wed, 20 Apr 2011 03:29:17 -0500, dantimatter <goo...@dantimatter.com>
wrote:


--
DrMaj...@yahoo.com

Bob Hanlon

unread,
Apr 21, 2011, 3:11:57 AM4/21/11
to
It's just more of the same.

areaOverlap[r1_, r2_, d_] =
r2^2 * ArcCos[(d^2 + r2^2 - r1^2)/(2 d r2)] +
r1^2 * ArcCos[(d^2 + r1^2 - r2^2)/(2 d r1)] -
Sqrt[(-d + r2 + r1) (d + r2 - r1) (d - r2 + r1) (d + r2 + r1)]/2;


separation[r1_?Positive, r2_?Positive, overlap_?NonNegative] /;
overlap <= (Min[r1, r2]^2 * Pi) :=
Chop[d /. FindRoot[
areaOverlap[r1, r2, d] == overlap,
{d, Max[r1, r2]}][[1]]];


venn[area1_?Positive, area2_?Positive, area3_?Positive,
overlap12_?NonNegative, overlap13_?NonNegative,
overlap23_?NonNegative] /;
(overlap12 <= Min[area1, area2] &&
overlap13 <= Min[area1, area3] &&
overlap23 <= Min[area2, area3]) :=
Module[{d12, d13, d23, x, y,
r1 = Sqrt[area1/Pi], r2 = Sqrt[area2/Pi], r3 = Sqrt[area3/Pi]},
d12 = separation[r1, r2, overlap12];
{x, y} = ({x, y} /. NSolve[{
Norm[{x, y}] == separation[r1, r3, overlap13],
Norm[{x, y} - {d12, 0}] == separation[r2, r3, overlap23]},
{x, y}][[1]]);


Graphics[{
Red, Circle[{0, 0}, r1],

Blue, Circle[{d12, 0}, r2],
Green, Circle[{x, y}, r3]}]];


venn[25, 16, 9, 0, 0, 0]

venn[25, 16, 9, 4, 3, 1]

With[{c = Pi/3 - Sqrt[3]/2}, venn[Pi, Pi, Pi, c, c, c]]


Bob Hanlon

---- dantimatter <goo...@dantimatter.com> wrote:

=============

Heike Gramberg

unread,
Apr 21, 2011, 3:12:29 AM4/21/11
to
I think you'll find that you can't make it work with three sets. There are
7 surface areas but there are only 6 parameters you
can play with, i.e. 3 radii and 3 distances between the centres of the circles which leaves one parameter short. You should
have a chance if you allow for shapes other than circles (ellipses maybe?), but that probably becomes even more fiddly.

Heike.

Mark McClure

unread,
Apr 21, 2011, 3:10:53 AM4/21/11
to
On Sun, Apr 17, 2011 at 7:54 AM, dantimatter <goo...@dantimatter.com> wrote:

> Is there a nice and easy way to make pretty Venn diagrams with
> Mathematica, where the areas of the circles and intersecting regions
> are to scale?

A student asked me how to *easily* generate Venn diagrams for a
presentation. I'm actually fairly adept at generating various images
with Mathematica but was a bit busy and turned to Wolfram|Alpha. The
results were pretty nice. In V8, you might just try the following:

wa = WolframAlpha[
"A intersect B union C intersect D",
{{"VennDiagram", 1}, "Content"}]

This generates a nice picture. Although I can't figure out a way to
get the code for the picture, you can manipulate it fairly easily.
Using this, I performed the following silliness:

g = Graphics @@ First[Cases[wa,
_GraphicsBox, Infinity]];
g = DeleteCases[g, _InsetBox, Infinity];
pos = Position[g, _RGBColor, Infinity];
g = MapAt[ColorData["StarryNightColors"][0] &,
g, pos[[{7}]]];
g = MapAt[ColorData["StarryNightColors"][1] &,
g, pos[[{1, 4}]]];
g = MapAt[ColorData["StarryNightColors"][0.5] &,
g, pos[[{2, 3, 5, 6}]]];
Show[g, ImageSize -> 500]


Of course, there's nothing like full programmatic control and I'm not
sure that this can meet your proportionality requirements. But it's
pretty easy!

Mark McClure

0 new messages