2
$\begingroup$

I have a rectangle of size $x \times n$ and need to draw the line that bisects the corners as percentages of the rectangle. The example plot shows a range from 0% to 100% in 5% increments. Can this be done in Mathematica? enter image description here

$\endgroup$
  • 1
    $\begingroup$ I'm not at a computer right now, but at first blush, I'd probably plot an appropriately scaled Floor[] function and use the Filling -> Axis option. Constructing a direct Polygon[], would be most efficient, but will require programming effort. $\endgroup$ – J. M. is in limbo Feb 26 '16 at 5:04
  • 2
    $\begingroup$ Quite a few ways to do this. Have a loot at DiscretePlot, also plotting unit steps or alternatively points with interpolation order->0 and the last two examples with filling to the axis. There should be examples of these here. $\endgroup$ – Mike Honeychurch Feb 26 '16 at 5:04
12
$\begingroup$
BarChart[
  Range[0, 100, 5],
  BarSpacing -> None,
  ChartStyle -> Directive[EdgeForm[None], FaceForm[Black]], 
  Axes -> False
]

Mathematica graphics

Many more alternative approaches are available. For instance, you can build the graphics directives yourself, as J.M. suggested:

Graphics[
  Table[Rectangle[{n, 0}, {(n + 5), n}], {n, 0, 95, 5}],
  AspectRatio -> 1/GoldenRatio
]

... or use DiscretePlot, as Mike suggested:

DiscretePlot[x, {x, 0, 100, 5},
  ExtentSize -> Left,
  PlotStyle -> Directive[PointSize[0], Black],
  FillingStyle -> Directive[Opacity[1], Black],
  Axes -> False
]
$\endgroup$
  • $\begingroup$ The Graphics solution worked well. Discrete plot adds a black line where 0 should be. Thanks! $\endgroup$ – R Hall Feb 26 '16 at 13:12
  • $\begingroup$ @RHall Yes DiscretePlot will draw an extent line even at zero; I also found that the extent line seems to slightly "overhang" the bar it corresponds to. It is the least visually pleasing result. BarChart seems to avoid that. Thanks for the accept as well! $\endgroup$ – MarcoB Feb 26 '16 at 13:19
4
$\begingroup$

Here is a solution done with the help of Riffle[] and ListLinePlot[]

diagram

down = {0, 0} + # {1.5, 1} & /@ Range[0, 10];
up = {0, 1} + # {1.5, 1} & /@ Range[0, 9];
ListLinePlot[Riffle[down, up], Filling -> Axis]

plot

Another simple method I think is using Floor[] directly.

Plot[Floor[x], {x, 0, 15}, Filling -> Axis]

plot

$\endgroup$
3
$\begingroup$
ListStepPlot[Range[0, 100, 5], Filling -> Axis, FillingStyle -> Black,
             PlotStyle -> Black, Ticks -> None, Axes -> False]

result of ListStepPlot

$\endgroup$
0
$\begingroup$

Here's my take:

raggedTriangle[pmin_?VectorQ, pmax_?VectorQ, p_: 5] := 
      Module[{d = pmax - pmin, iv}, iv = Subdivide[#, 100/p] & /@ d;
             Polygon[Append[TranslationTransform[pmin] /@ 
                            Most[Riffle[Transpose[iv],
                                        Transpose[MapAt[RotateLeft, iv, 2]]]],
                            {First[pmax], Last[pmin]}]]]

An example:

Graphics[{{Blue, Rectangle[{1, 2}, {5, 3}]},
          {Orange, raggedTriangle[{1, 2}, {5, 3}, 10]}}]

the steps go up and up

$\endgroup$
  • $\begingroup$ I get the error "Transpose::nmtx: The first two levels of {Subdivide[4,10],Subdivide[1,10]} cannot be transposed. In both 9 & 10 $\endgroup$ – R Hall Feb 26 '16 at 13:15
  • $\begingroup$ Subdivide[] first became available in version 10.1. A substitute for earlier versions would be subdivide[xmax_: 1, n_Integer?Positive] := xmax Range[0, 1, 1/n]. $\endgroup$ – J. M. is in limbo Feb 26 '16 at 13:20
  • $\begingroup$ Thanks @J.M. Looks like I'll need to upgrade $\endgroup$ – R Hall Feb 26 '16 at 13:51
  • 1
    $\begingroup$ Nevertheless, if you make the substitution I indicated, then there's no need for you to upgrade yet just for this simple thing. ;) Upgrade only if you want all the other neat stuff that came after 10. $\endgroup$ – J. M. is in limbo Feb 26 '16 at 13:59

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.