1
$\begingroup$

I want to draw the following picture in Mathematica as shown below with distinct color for each section.

enter image description here

$\endgroup$
  • $\begingroup$ Well, did you try anything and, if yes, where did you get stuck? $\endgroup$ – acl Jul 16 '14 at 11:25
  • 1
    $\begingroup$ I used this code: Plot3D[-1/4 x^3, {x, -20, 20}, {y, 0, 30}, ColorFunction -> Function[{x}, Piecewise[{{Red, -20 <= x < -10}, {Orange, -10 <= x < 0}, {Yellow, 0 <= x <= 20}}]], PlotStyle -> Thick, ColorFunctionScaling -> False] $\endgroup$ – SKMohammadi Jul 16 '14 at 11:32
10
$\begingroup$

Try using MeshShading:

Plot3D[
 -(x - 12.5)^3,
 {x, 0, 25},
 {y, 0, 20},
 MeshShading -> {Table[Hue[x], {x, 0, 1, 1/16}]}
 ]

The Table part generates sixteen different colors from the Hue color function. I did this because the image in this case had sixteen rows. If there are more rows than colors the colors will be reused cyclically.

Mesh shading

$\endgroup$
  • $\begingroup$ Thanks you. and about dotted grid lines in background? $\endgroup$ – SKMohammadi Jul 16 '14 at 12:13
  • $\begingroup$ @MathMan I was away, it seems Mr.Wizard solved it in the meantime :) $\endgroup$ – C. E. Jul 16 '14 at 14:32
6
$\begingroup$

Building on Pickett's answer with a few more Options:

pl = Plot3D[-(x - 12.5)^3, {x, 0, 25}, {y, 0, 20}, 
 MeshShading -> {Table[Hue[x], {x, 0, 1, 1/16}]},
 BoxRatios -> 1, 
 Boxed -> False,
 FaceGrids -> {{0, 1, 0}, {-1, 0, 0}, {0, 0, -1}}, 
 FaceGridsStyle -> Directive[Thick, Gray, Dotted]
]

enter image description here

It's not perfect but hopefully it points you in a useful direction.


An example for the legend was requested. One can use Inset to place a Graphics (or other) object in a 3D plot. As a rough illustration:

rec = Graphics[{Green, EdgeForm[Thick], Rectangle[{0, 0}, {50, 20}]}, 
   ImageSize -> {50, 20}, PlotRangePadding -> 0];

lg = Framed[Row[{rec, Style["S", 18]}]];

Show[pl, Epilog -> Inset[lg, Scaled[{0.8, 0.8}]]]

enter image description here

$\endgroup$
  • $\begingroup$ I copied the wrong code for that particular graphic. Now corrected. Sorry for any confusion. $\endgroup$ – Mr.Wizard Jul 16 '14 at 13:16
  • $\begingroup$ and about the Legend? $\endgroup$ – SKMohammadi Jul 16 '14 at 16:48
  • $\begingroup$ @MathMan Which version of Mathematica are you using? $\endgroup$ – Mr.Wizard Jul 16 '14 at 23:44
  • $\begingroup$ I am using Mathematica 7 $\endgroup$ – SKMohammadi Jul 19 '14 at 11:07
  • $\begingroup$ @MathMan See update. $\endgroup$ – Mr.Wizard Jul 19 '14 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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