3
$\begingroup$

I would like to plot bars with specific thickness for each bar like that: enter image description here

Is there an easy way to do it?

What I have tried:

Plot[{Evaluate[y = 0], Evaluate[y = 1], Evaluate[y = 2]}, {x, -10, 
  10}, PlotStyle -> Thickness[0.035], PlotRange -> {-10, 10}, 
 Axes -> None]
$\endgroup$
1
  • $\begingroup$ see GridLines, GridLinesStyle $\endgroup$
    – Kuba
    Feb 21, 2018 at 17:53

2 Answers 2

4
$\begingroup$
values = Sort@RandomSample[Range[10], 6];
colors = {Yellow, Orange, Red, Purple, Magenta, Blue};

RectangleChart

RectangleChart[Thread[{10, Join[{First @ values}, Differences @ values]}], 
 ChartStyle -> "Pastel", PerformanceGoal -> "Speed", 
 ChartLayout -> "Stacked", ChartBaseStyle -> EdgeForm[], Axes -> False]

enter image description here

Use ChartStyle -> colors to get

enter image description here

BarChart

BarChart[Join[{First@values}, Differences@values], 
 ChartStyle -> "Pastel", PerformanceGoal -> "Speed",  ChartLayout -> "Stacked", 
 ChartBaseStyle -> EdgeForm[], Axes -> False, PlotRange -> {{.5, 1}, Automatic}]

enter image description here

Use ChartStyle -> colors to get

enter image description here

StackedListPlot (v11.2)

StackedListPlot[Thread /@ Thread[{{0, 1}, Join[{First @ values},
   Differences @ values]}, List, {2}], Axes -> False, PlotStyle -> None] /. 
 Opacity[.2] -> Opacity[1]

enter image description here

Use PlotStyle -> colors and post-process to remove the lines:

StackedListPlot[Thread /@ Thread[{{0, 1}, Join[{First @ values},
    Differences @ values]}, List, {2}], Axes -> False, PlotStyle -> colors] /.
 Opacity[.2] -> Opacity[1] /. Line[__] :> {}

enter image description here

Graphics + Rectangle

rects = Rectangle[{0, #}, {10, #2}]& @@@ Partition[Join[{0},values], 2, 1];
colors = {Yellow, Orange, Red, Purple, Magenta, Blue};
Graphics[Transpose[{colors, rects}], AspectRatio -> 1/ GoldenRatio]

enter image description here

$\endgroup$
4
  • $\begingroup$ thanks a lot ! How can I change the color ? Could you please specify this specifically ? $\endgroup$
    – james
    Feb 22, 2018 at 20:10
  • $\begingroup$ @james, please see the updated version. Replace colors with your choice of colors. $\endgroup$
    – kglr
    Feb 22, 2018 at 20:35
  • $\begingroup$ nice ! Thanks a lot! $\endgroup$
    – james
    Feb 22, 2018 at 21:07
  • $\begingroup$ @james, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Feb 23, 2018 at 2:55
3
$\begingroup$

You could plot straight lines, and fill in between them like this,

nbars = 5;
values = {1, 5, 6, 7, 10};
Plot[values,
    {x, -5, 5},
    Filling -> Thread[Range[nbars] -> Prepend[Range[nbars - 1], Bottom]],
    FillingStyle -> Directive[Opacity[1]],
    PlotStyle -> None, Axes -> None
]

enter image description here

$\endgroup$
1
  • $\begingroup$ thanks a lot ! How can I change the color ? $\endgroup$
    – james
    Feb 22, 2018 at 7:42

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.