# How to group(merge) several plot parts?

parta = ContourPlot[
y == (Sqrt[Abs[x]] + Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2},
PlotPoints -> 100, MaxRecursion -> 1];
partb = ContourPlot[
y == (Sqrt[Abs[x]] - Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2},
PlotPoints -> 100, MaxRecursion -> 1];
heart = JoinedCurve[parta, partb];
Show[heart, PlotRange -> All]


I want to join(merge) parta and partb to heart,then I can rotate or move only one object heart.But JoinedCurve doesn't join parta and partb.

How to solve this problem?

• You have to take the == out of Piecewise. Commented Mar 8, 2018 at 2:39
• @JEM_Mosig,thanks for your reply!I updated the question,revised the codes,please check it. Commented Mar 8, 2018 at 3:43
• You don't need JoinedCurve. Just evaluate Show[parta, partb] Commented Mar 8, 2018 at 4:39
• If I use Show[parta, partb],I need to rotate or move 2 parts.If I can merge parta and partb to heart,there's only one object I need to care. Commented Mar 8, 2018 at 5:36

This should do it:

ContourPlot[
Piecewise[{
{2*x^2 - 2*x*y + y^2, x > 0},
{2*x^2 + 2*x*y + y^2, x <= 0}
}] == 1,
{x, -1, 1}, {y, -1, 1.5},
PlotPoints -> 100,
MaxRecursion -> 1
]


If you must use JoinedCurve, then you have to apply JoinedCurve to the graphics primitives, i.e. Line objects. This can be done as follows.

Evaluate the code in your question to define parta and partb. Note that ContourPlot returns a Graphics with a GraphicsComplex inside:

parta // InputForm // Shallow
(* Graphics[GraphicsComplex[<<2>>], {<<11>>}] *)


Then extract the graphics primitives and the coordinates from the GraphicsComplex and combine them:

With[{
(* find the line object in the first figure *)
aLine = FirstCase[parta, Line[___], Null, Infinity],
(* extract the coordinates of the points of that line *)
aCoords = FirstCase[parta, GraphicsComplex[coords_, ___] :> coords, Null, Infinity],
(* find the line object in the second figure *)
bLine = FirstCase[partb, Line[___], Null, Infinity],
(* extract the coordinates of the points of that line *)
bCoords = FirstCase[partb, GraphicsComplex[coords_, ___] :> coords, Null, Infinity]
},
Graphics[
GraphicsComplex[
Join[aCoords, bCoords],
JoinedCurve[{
{aLine},
(* we have to shift the integers that represent the points,
because the coordinates of the second line begin after the
coordinates of the first line *)
{bLine /. n_Integer :> n + Length[aCoords]}
}, CurveClosed -> False
]
]
]
]


Of course, if you really just want to create the figure and are not interested in the graphics primitive of the curve, then your code almost works - just replace JoinedCurve by {}:

parta = ContourPlot[
y == (Sqrt[Abs[x]] + Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2},
PlotPoints -> 100, MaxRecursion -> 1];
partb = ContourPlot[
y == (Sqrt[Abs[x]] - Sqrt[1 - x^2]), {x, -2, 2}, {y, -2, 2},
PlotPoints -> 100, MaxRecursion -> 1];

Show[{parta, partb}, PlotRange -> All]

• Your answer works well.I am curious why mathematica has no function to do it. Commented Mar 8, 2018 at 4:29
• @kittygirl: Normally you don't need the graphics primitive, but just the graphics itself. In that case you can use Show without JoinedCurve, as m_goldberg has suggested. Commented Mar 8, 2018 at 19:45