4
$\begingroup$

I have some grid-lines I want to extend outside of the bounding box of 3D plot BarChart3D. I do want to keep the bounding box small, as I want the automatic axes on the bounding box edge remain next to data.

In case of 2D plots ImagePadding does the trick. In Plot3D it does not. Here is an example.

ρ = 1/2 ({{1, 0, 0, -1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 1}});
Show[{
  BarChart3D[ρ, BarSpacing -> {1, 1}, ChartLayout -> "Grid", 
   FaceGrids -> None, Method -> {"Canvas" -> None}, 
   PlotRange -> {{-1, 8}, {-1, 8}, {-0.5, 0.5}}, Boxed -> True, 
   FaceGrids -> None, Axes -> {True, True, True}, 
   ViewPoint -> {1, -5, 3}, 
   Ticks -> {Range[-10, 10, 2], Range[-10, 10, 2], 
     Range[-10, 10, 2]}], 
  Graphics3D[{Table[Text[y, {10, y 2 - 1.5, 0}], {y, 1, 4}], 
    Table[Line[{{-4, y 2 - 1.5, 0}, {20, y 2 - 1.5, 0}}], {y, 1, 
      4}]}]}, PlotRangeClipping -> False, ImagePadding -> All
 ]

bar chart clipping

If I add PlotRange->All I see, that the manual grid lines were clipped. I want the long lines, but BoundingBox tightly around the bars.

bar chart stretched

There exists a trivial solution for same problem for 2D plots: Drawing outside Plot area

I do expect, there is a solution, which does not require re-implementation of axes, face grids and other features.

$\endgroup$
  • $\begingroup$ There was an answer, where the axes and bounding box were manually implemented. The author deleted it. Although I was looking for a way of avoiding re-implementation of Mathematica built in features the answer should remain, as might lead another reader. Someone should recover the answer if possible. $\endgroup$ – Johu Jan 19 '16 at 11:15
  • $\begingroup$ I deleted it because I thought it didn't answer the question, but I put it back... $\endgroup$ – Jason B. Jun 25 '16 at 3:42
3
$\begingroup$

Here is one way to do it. The strategy is to create our own bounding box Graphics3D object, and then give all the important options to Show instead of BarChart3D

Module[{barchart, linesandlabels, box},
 barchart =  BarChart3D[\[Rho], BarSpacing -> {1, 1},
    ChartLayout -> "Grid", 
    FaceGrids -> None,
    Method -> {"Canvas" -> None} ];
 linesandlabels = Graphics3D[{
    Table[Text[y, {10, y 2 - 1.5, 0}], {y, 1, 4}], 
    Table[Line[{{-4, y 2 - 1.5, 0}, {20, y 2 - 1.5, 0}}], {y, 1, 4}]}];
 box = Graphics3D[{Directive[
     FaceForm[None],
     EdgeForm[Black]],
    Cuboid[{-1, -1, -.5}, {8, 8, .5}]}];
 Show[barchart, linesandlabels, box,
  Boxed -> False,
  Axes -> True,
   Ticks -> {Range[-10, 8, 2], Range[-10, 8, 2], 
         Range[-10, 10, 2]},
  PlotRange -> {{-1, 20}, {-1, 8}, {-0.5, 0.5}},
  AxesOrigin -> {-1, -1, -.5},
  AxesStyle -> Opacity[0],
  TicksStyle -> Opacity[1]
  ]
 ]

enter image description here

This feels like a bit of a kludge, but works for this situation. If you want to do it more generally, we could possibly make a plotting function out of it.

|improve this answer|||||
$\endgroup$
  • 1
    $\begingroup$ +1 for the effort, but implementing the axes manually was exactly, what I though I should be able to avoid. $\endgroup$ – Johu Jan 18 '16 at 17:33

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.