3
$\begingroup$

I am trying to use different colours to plot components of a ContourPlot in Mathematica. The contours are sufficiently complicated, such that I do not expect there to be a simple parametrisation. In my example

lim = 0.6; m = 1/6;
comp1 = -3 + 48 m^2 - 16 x - 128 m^2 x + 64 x^2 + 64 y^2;
comp2 = 81 m^2 - 1944 m^4 + 165888 m^8 - 27 x + 20736 m^4 x - 
   221184 m^6 x + 144 x^2 - 1728 m^2 x^2 + 27648 m^4 x^2 - 
   589824 m^6 x^2 - 576 x^3 - 36864 m^2 x^3 + 737280 m^4 x^3 + 
   6144 x^4 - 86016 m^2 x^4 + 688128 m^4 x^4 + 12288 x^5 - 
   786432 m^2 x^5 + 65536 x^6 - 262144 m^2 x^6 + 262144 x^7 - 
   2880 m^2 y^2 + 9216 m^4 y^2 + 589824 m^6 y^2 - 1344 x y^2 + 
   24576 m^2 x y^2 - 49152 m^4 x y^2 + 10240 x^2 y^2 - 
   303104 m^2 x^2 y^2 - 720896 m^4 x^2 y^2 + 40960 x^3 y^2 - 
   262144 m^2 x^3 y^2 + 131072 x^4 y^2 - 262144 m^2 x^4 y^2 + 
   786432 x^5 y^2 + 12288 m^2 y^4 + 688128 m^4 y^4 + 28672 x y^4 + 
   524288 m^2 x y^4 + 65536 x^2 y^4 + 262144 m^2 x^2 y^4 + 
   786432 x^3 y^4 + 262144 m^2 y^6 + 262144 x y^6;
c0 = ContourPlot[comp1 comp2 == 0, {x, -lim, lim}, {y, -lim, lim}, 
   PlotPoints -> 200, ContourStyle -> Gray, ImageSize -> Small, 
   Axes -> True];
c1 = ContourPlot[comp1 == 0, {x, 0, lim}, {y, -lim, lim}, 
   ContourStyle -> Blue];
c2 = ContourPlot[comp1 == 0, {x, -lim, 0}, {y, -lim, lim}, 
   ContourStyle -> Red];
Show[c0, c1, c2]

I study the zero locus of a polynomial in $x$ and $y$, with $m>0$ as a parameter. The contour has various intersection points that partitions it into (here 7) components. I would like to plot each of these components in a different colour. Since the polynomial factorises into comp1 and comp2, this can be done for one factor comp1. In the picture, the blue and red components can be coloured by restricting the plotting domain. The other factor comp2 contains multiple components, but does not factor for generic $m>0$ (also not for the chosen $m=\frac16$). I was wondering whether there is a trick to give the remaining 5 components belonging to comp2 own colour. Many thanks for any suggestions!

enter image description here

$\endgroup$

1 Answer 1

5
$\begingroup$

A hack that works for the example in OP: Use the option Exclusions -> {x == 0} to add a gap in contour lines at x = 0 (which happens to be the intersection of the contours for comp1 == 0 and comp2 == 0 in this example), and post process to insert a different color before each line piece:

cp = ContourPlot[comp1 comp2 == 0, {x, -lim, lim}, {y, -lim, lim}, 
   ImageSize -> Medium, PlotPoints -> 150, Axes -> True, 
   Exclusions -> {x == 0}];

colors = ColorData[97] /@ Range[7];

Row[{cp, cp /. l_Line :> {Thick, Last[colors = RotateLeft[colors]], l}}, 
 Spacer[10]]

enter image description here

Alternatively, we can use comp2 == 0 to define an ImplicitRegion and use it with RegionPlot (which gives the correct number of multiple lines whereas ContourPlot of comp2 == 0 gives two closed curves):

impregion2 = ImplicitRegion[comp2 == 0, {{x, -lim, lim}, {y, -lim, lim}}];

colors = ColorData[97] /@ Range[7]; 

Show[RegionPlot[impregion2, ImageSize -> Large, PlotPoints -> 200, 
   Axes -> True, PlotRange -> {{-.6, .6}, {-.6, .6}}], 
  ContourPlot[comp1 == 0, {x, -lim, lim}, {y, -lim, lim}, 
   PlotPoints -> 200, Axes -> True, Exclusions -> {x == 0}]] /. 
 l_Line :> {Thick, Last[colors = RotateLeft[colors]], l}

enter image description here

$\endgroup$
1
  • $\begingroup$ Many thanks! I would like to exclude further points (given by coordinates {x,y}from the level set), such that there are even more colours (I omitted this for simplicity of the question). Do you know how this can be done with Exclusions? Also, Exclusions produces quite large gaps around the excluded points, and the lines don't touch the excluded points. Would you know how this can be fixed? $\endgroup$
    – El Rafu
    May 26, 2021 at 11:44

Your Answer

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

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