Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to have "Tufte" style figure axes? That is so that the axes do not need to connect or span the entire data range?

For an example of what I mean from the R's default histogram style:

enter image description here

I know how to get the ticks out etc ... but not how to break the axes at the origin or only draw the axes/frame for a subset of the graphic range.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

You can generate the axes separately using an "empty" Plot.

As you said you know how to specify the ticks, so I'll not bother to do it, but just show a simple example about what I mean:

hsgm = Histogram[RandomVariate[NormalDistribution[0, 1], 1000]]

axes = Plot[I, {x, 0, 1}, (* the range of x is not important *)
    AxesOrigin -> {-4, -10},
    PlotRange -> {{-3, 3}, {0, 200}}
    ] // FullGraphics;

Then combine them:

Show[{hsgm /. (Axes -> _) :> (Axes -> False), axes}, PlotRange -> All]

disconnected axes graphics

Note: FullGraphics is well-known to be a buggy function, but if you find it usable, I'll glad to mention my more detailed and interesting post on it.

Update

To add labels like FrameLabel, we can't use Frame or we'll break the "disconnected" appearence. Thus we have to add them manually.

Here we use the completePlotRange function invented by @AlexeyPopkov to detect the plot range of the histogram, and automatically generate the final plot.

Clear[myHistogram]
Options[myHistogram] = {"axesLabels" -> {}};
myHistogram[data_, OptionsPattern[]] := 
 Module[{axesLabels, axesOriginFunc, hsgm, range, axes, labels, aspr = 1/GoldenRatio},
        axesLabels = OptionValue["axesLabels"];
        axesOriginFunc[{min_, max_}, p_: 0.1] := min - p (max - min);
        hsgm = Histogram[data, Axes -> False, PlotRangePadding -> None];
        range = Through[{Min, Max}@FindDivisions[#, 10]] & /@ completePlotRange[hsgm];
        axes = Plot[I, {x, 0, 1},
                    AxesOrigin -> MapThread[axesOriginFunc, {range, {0.1 aspr, 0.1}}],
                    PlotRange -> range] //
               FullGraphics// (*disable Antialiasing to make the axes and ticks un-blur*)
               # /. Line[pts__] :> Style[Line[pts], Antialiasing -> False] &;
        If[axesLabels =!= {},

           labels = Graphics[{
                              Text[axesLabels[[1]],
                                   {Mean[range[[1]]], axesOriginFunc[range[[2]]]},
                                   {0, 5}],
                              Text[axesLabels[[2]],
                                   {axesOriginFunc[range[[1]], 0.1 aspr], Mean[range[[2]]]},
                                   {0, -5}, {0, 1}]
                             }];
           Show[{hsgm, axes, labels}, AspectRatio -> aspr, PlotRange -> All],

           Show[{hsgm, axes}, AspectRatio -> aspr, PlotRange -> All]
         ]
  ]

Usage:

myHistogram[
            RandomVariate[NormalDistribution[0, 1], 1000],
            "axesLabels" -> (Style[#, Bold, 14, Darker[Red]] & /@ {"x", "Frequency"})
           ]

disconnected frame example 2

share|improve this answer
    
So great! Thank you –  Gabriel Dec 10 '13 at 13:36
    
@Gabriel Glad I can help! –  Silvia Dec 10 '13 at 13:36
    
@Silva I am working through the details of this answer and I wonder if you could give an example that uses Frame instead of Axes ... as labeling the axes puts the label in the "wrong" place (at the end, not under). I worry that the AxesOrigin is key to getting the broken effect I want ... but then I don't know how to label the axes! –  Gabriel Dec 10 '13 at 16:27
    
also does your other post cover all/most of the ways in which FullGraphics is buggy, or are there more? –  Gabriel Dec 10 '13 at 23:10
    
@Gabriel Yes the AxesOrigin is the key, so there is no way to realize this by Frame. I think you'll have to place the labels manually, say by Text[ ]. I'll update the answer after breackfast. About the FullGraphics, I suggest you search it over this site to have a more general sight. –  Silvia Dec 11 '13 at 8:36
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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