4
$\begingroup$

I have the following code to draw a convex hull.

P0={0,0};
P1={1,0}; 
P2={-1,0}; 
R[1]=ConvexHullMesh[{P0,P1,P3},Frame-> True,PlotRange->Automatic ]
Show[Region[R[1],PlotRange->{{-1.2,1.2},{-1.2,1.2}},Axes->True,ImageSize->Medium],Graphics[Text["(0,0)",{0,0}]],Graphics[Text["(0,1)",{0,1}]],Graphics[Text["(1,0)",{1,0}]],Graphics[Text["Choice [1,2] and [6,2]",{0.7,1}]]]

Now I want to change the color of the convex hull. I tried PlotStyle but it was not working. Can you suggest me an option to change the color from the default light blue? I'm very new to drawing plots in Mathematica. You can also give me advice on how to improve my drawing for future. Thanks.

$\endgroup$

1 Answer 1

4
$\begingroup$

To style primitives in a MeshRegion object you can use MeshCellStyle:

P0 = {0, 0};
P1 = {1, 0};
P2 = {0, 1};
R[1] = ConvexHullMesh[{P0, P1, P2}, Frame -> True, 
  PlotRange -> Automatic, MeshCellStyle -> {2 -> Opacity[.5, Red]}]

enter image description here

Show[Region[R[1], PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}, 
  Axes -> True, ImageSize -> Medium], Graphics[Text["(0,0)", {0, 0}]],
  Graphics[Text["(0,1)", {0, 1}]], Graphics[Text["(1,0)", {1, 0}]], 
 Graphics[Text["Choice [1,2] and [6,2]", {0.7, 1}]]]

enter image description here

An alternative way to add text labels using the options Epilog and MeshCellShapeFunction:

ConvexHullMesh[{P0, P1, P2}, Frame -> True, Axes -> True, 
 MeshCellStyle -> {2 -> Opacity[.5, Red]}, 
 PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
 MeshCellShapeFunction -> 
    {{0, 1 | 2} -> (Text[Style[Round@#, Opacity[1], Black, 12], #, {0, 1.2}] &),
     {0, 3} -> (Text[Style[Round@#, Opacity[1], Black, 12], #, {0, -1.2}] &)}, 
 Epilog -> Text["Choice [1,2] and [6,2]", {0.7, 1}]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks a lot. Is it also possible to increase the font size of the axis coordinates? Because when I make the plot smaller it is hard to read the coordinates unless we zoom in. $\endgroup$
    – Epsilon
    Commented Apr 18, 2020 at 15:17
  • 1
    $\begingroup$ @Sumit, you can add the option FrameTicksStyle -> 14 or the option LabelStyle -> 14 . $\endgroup$
    – kglr
    Commented Apr 18, 2020 at 15:22
  • $\begingroup$ Thanks. It worked. $\endgroup$
    – Epsilon
    Commented Apr 18, 2020 at 16:58

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.