1
$\begingroup$

I am working on using Venn diagram to explain logic and logical connections between sets. After coming upon these answers: Create a Venn Diagram, How to plot Venn diagrams with Mathematica?, which I found very helpful I was wondering if there is a way to convert the statement All Bananas are Tasty. (All Bananas are elements of Tasty or Bananas $\iff$ Tasty) and All Apples are Tasty (Apples $\iff$ Tasty) into a Venn diagram with three circles labeled?

The difference between this question and the questions above is I am wondering how to label an area of the Venn diagram, as well as how to instead of using $A_1$ and $A_2$ as values as explained in this good well-explained answer https://mathematica.stackexchange.com/a/2557/76873 by user https://mathematica.stackexchange.com/users/495/fjra use the values Bananas, Apples, and Tasty.

I am also wondering if there is a way to color the Venn diagram, possibly using PlotStyle or a graphics primitive?

So far I have tried:

 Module[{i, r = .6, R = 1, v, grouprules, x, y, x1, x2, y1, y2, ve}, 
  v = Table[Circle[r {Cos[#], Sin[#]} &[2 Pi (i - 1)/n], R], {i, n}];
  {x1, x2} = {Min[#], Max[#]} &[
    Flatten@Replace[v, 
      Circle[{xx_, yy_}, rr_] :> {xx - rr, xx + rr}, {1}]];
  {y1, y2} = {Min[#], Max[#]} &[
    Flatten@Replace[v, 
      Circle[{xx_, yy_}, rr_] :> {yy - rr, yy + rr}, {1}]];
  ve[x_, y_, i_] := 
   v[[i]] /. Circle[{xx_, yy_}, rr_] :> (x - xx)^2 + (y - yy)^2 < rr^2;
  grouprules[x_, y_] = 
   ineqs /. Table[
     With[{is = i}, Subscript[_, is] :> ve[x, y, is]], {i, n}];
  Show[If[MatchQ[ineqs, {} | False], {}, 
    RegionPlot[grouprules[x, y], {x, x1, x2}, {y, y1, y2}, 
     Axes -> False]], Graphics[v], 
   PlotLabel -> 
    TraditionalForm[Replace[ineqs, {} | False -> \[EmptySet]]], 
   Frame -> False]]
p1 = Bananas \[Equivalent] Tasty
p2 = Apples \[Equivalent] Tasty

VennDiagram2[3, 
 And[Bananas \[Equivalent] Tasty , Apples \[Equivalent] Tasty]]

Here is what I got: Result of Venn Diagram

EDIT (thanks to User criedhne): I was able to get this code:

Bananas = Disk[{0, 1}];
Apples = Disk[{-0.5, 0}];
Tasty = Disk[{0.5, 0}];
subsets = Subsets[{Bananas, Apples, Tasty}, {1, 3}];
subsetscolors = 
  Map[Function[{c}, 
    Blend[Flatten[
      Map[Table[Map[Append[#, 1.5/Length[c]] &, c], 2] &, c]]]], 
   Subsets[Map[ColorData[112], Range[3]], {1, 4}]];
RegionPlot[
 Evaluate[DiscretizeRegion[
     RegionDifference[BooleanRegion[And, #], 
      BooleanRegion[Or, 
       Complement[{Bananas, Apples, Tasty, EmptyRegion[2]}, #]]]] & /@
    subsets], 
 PlotLabels -> 
  Callout[(StringJoin @@@ 
     Subsets[{"Bananas", "Apples", "Tasty"}, {1, 3}]), Center], 
 Sequence[PlotStyle -> subsetscolors, 
  BoundaryStyle -> Directive[Thickness[0.01], White], Frame -> False, 
  LabelStyle -> {24}, PerformanceGoal -> "Speed", ImageSize -> 450]]

My question is how to change the example Venn diagram's complex code to reflect the two logical statements mentioned above.

[2]: https://i.stack.imgur.com/XMGZD.png

$\endgroup$
4
  • $\begingroup$ What have you tried so far? Can you apply the code from those answers to at least give us a starting point diagram that expresses your logic correctly? The rest is formatting and it may not be that hard once you have the diagram set up. $\endgroup$
    – MarcoB
    Jan 21, 2021 at 3:38
  • $\begingroup$ I have updated my answer in response to your question. I hope this helps. $\endgroup$ Jan 21, 2021 at 13:15
  • 2
    $\begingroup$ Have you seen this article, Venn Diagram - Improved Visualization Labeling? It demonstrates adding color to Venn diagrams, and how to use new features included with Mathematica version 12 to easily add labels to intersecting subsets. $\endgroup$
    – creidhne
    Jan 22, 2021 at 2:53
  • $\begingroup$ Thank you! That's very helpful. I was wondering if someone could explain what the code means. I want to be able to modify the code to reflect the first-order logical statement, "All Bananas are Tasty" and "All apples are Tasty." I have updated my answer to include my changes/attempt to go from logic to Venn diagram. $\endgroup$ Jan 22, 2021 at 13:53

1 Answer 1

2
$\begingroup$

The updated code is a specific example where the sets A, B and C are mutual subsets, that is, each set has an intersection with every other set. The first part of the code reflects this arrangement with overlapping discs.

a = Disk[{0, 1}];
b = Disk[{-0.5, 0}];
c = Disk[{0.5, 0}];
Graphics[{EdgeForm[{Thick, Black}], FaceForm[Opacity[0]], a, b, c}]

intersection of three sets

This isn't what we need, so we need to change the disks to match the assertion that all apples are tasty and all bananas are tasty. Instead, we might say that all apples are a subset of tasty (although real-world experience tells us that not all apples are tasty). Similarly, we might say all bananas are a subset of tasty. Moreover, real-world experience tells us that apples are not bananas. So, except the abstract sense, there is no intersection between apples and bananas, but we may assume all apples and all bananas are tasty. We change the diagram to match the new sets, and now the outer disk represents tasty, and the small disks are apples and bananas, but the diagram is kinda dull and unenlightening.

a = Disk[{0, 1}];
b = Disk[{-0.5, 1}, 0.5];
c = Disk[{0.5, 1}, 0.5];
Graphics[{EdgeForm[{Thick, Black}], FaceForm[Opacity[0]], a, b, c}]

two disjoint sets

If we agree that some apples (selected varieties) and some bananas (the unripe ones) are not tasty, then the diagram becomes:

a = Disk[{0, 1}];
b = Disk[{1, 0}];
c = Disk[{-1, 0}];
Graphics[{EdgeForm[{Thick, Black}], FaceForm[Opacity[0]], a, b, c}]

disjoint sets for tasty apples and bananas

where the upper disk represents tasty, and the left and right disks are apples and bananas.

The next section of the code uses Subsets to divide the disks into sections that represent the overlapping logical subsets. We don't have a subset where all sets overlap, and we need new callouts, so:

subsets = Most@Subsets[a, b, c, {1, 2}];
callouts = Flatten@{"Tasty","Apples","Bananas",StringRiffle[{"Tasty",#},"\n"]
  &/@{"Apples","Bananas"}};

Putting all the parts together, we get:

a = Disk[{0, 1}];
b = Disk[{1, 0}];
c = Disk[{-1, 0}];
subsets = Most@Subsets[{a, b, c}, {1, 2}];
callouts = 
  Flatten@{"Tasty", "Apples", "Bananas", 
    StringRiffle[{"Tasty", #}, "\n"] & /@ {"Apples", "Bananas"}};
subsetscolors = 
  Map[Function[{c}, 
    Blend[Flatten[
      Map[Table[Map[Append[#, 1.5/Length[c]] &, c], 2] &, c]]]], 
   Subsets[Map[ColorData[112], Range[3]], {1, 4}]];
RegionPlot[
 Evaluate[DiscretizeRegion[
     RegionDifference[BooleanRegion[And, #], 
      BooleanRegion[Or, 
       Complement[{a, b, c, EmptyRegion[2]}, #]]]] & /@ subsets], 
 PlotLabels -> Callout[callouts, Center], 
 Sequence[PlotStyle -> subsetscolors, 
  BoundaryStyle -> Directive[Thickness[0.01], White], Frame -> False, 
  LabelStyle -> {24}, PerformanceGoal -> "Speed", ImageSize -> 450, 
  AspectRatio -> Automatic]]

venn diagram

The RegionDifference code section is the part of the code that divides the sets into sections that are colored and labeled. Look at the result of this part of the code to see how the sets are divided into subsets.

Evaluate[DiscretizeRegion[
    RegionDifference[BooleanRegion[And, #], 
     BooleanRegion[Or, Complement[{a, b, c, EmptyRegion[2]}, #]]]] & /@
   subsets]

I hope I've explained the code in enough detail.

$\endgroup$
3
  • $\begingroup$ Does Mathematica have functionality for making Venn diagrams out of logical statements and relating Venn diagrams to first-order logic? I'm thinking about requesting it because Venn diagrams can be very helpful for logic and discrete structures. It seems like a lot of code to just translate the statement "All bananas are Tasty" and "All Apples are tasty" into a diagram so I was wondering if there's some possibility of Wolfram adding built-in modules or functionality. In response to your question, yes the code is explained in enough detail. $\endgroup$ Jan 24, 2021 at 22:27
  • $\begingroup$ @PeterBurbery Check the Wolfram Demonstrations Project for Venn diagram applications. The demonstrations include code you can modify for your needs. My opinion: I'm impressed that so little Mathematica code performs complex logic applications such those needed for Venn diagrams. The Wolfram Language already supports extensive Logic and Boolean Algebra operations. $\endgroup$
    – creidhne
    Jan 31, 2021 at 19:05
  • 1
    $\begingroup$ @PeterBurbery I imagine that Wolfram could add dedicated support for Venn diagrams. You might contact Wolfram Product Feedback to offer your suggestions. $\endgroup$
    – creidhne
    Jan 31, 2021 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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