4
$\begingroup$

I'm trying to essentially make a scatter plot where instead of points, the markers are bars that connect the point to the bottom of the graph (like a histogram). My problem is that my graph has something like 30 points and the x-axis labels are smashed together. I tried doing it with Histograms to no avail, and tried doing it with BarChart with a bit more success, but there are so many markers on the x-axis that you can't read them. My problems would be solved if I could specify only a few markers at certain positions. Can anyone help?

Below is what the graph looks like: I think it's pretty self-explanatory what the problem is even just by looking at it.enter image description here

$\endgroup$
  • 1
    $\begingroup$ Have you looked at the Filling option of ListPlot[]? $\endgroup$ – J. M. is away Feb 19 '16 at 6:25
  • 1
    $\begingroup$ What @J.M. suggested plus make your own x axis tick labels or tick function $\endgroup$ – Mike Honeychurch Feb 19 '16 at 6:28
  • 1
    $\begingroup$ Try plotting your data with ListStepPlot, use the "Center" arguement. $\endgroup$ – Quantum_Oli Feb 19 '16 at 8:56
6
$\begingroup$

It's a good question and something I've dealt with recently when essentially trying to plot a histogram but when I already have the data binned. After using ListLinePlot with InterpolationOrder->0 I discovered the very useful ListStepPlot function. Allow me to demonstrate.

If we have some samples we can just use Histogram:

    samples = RandomVariate[SkewNormalDistribution[4, 1, 2], 100000];
    Histogram[samples, {0, 10, 0.1}, "Probability"]

enter image description here

However, if you already have the data points corresponding to the elements of the histogram, as in your case.

    {binEdges, counts} = HistogramList[samples, {0, 10, 0.1}, "Probability"];
    dataPoints = Transpose[{MovingAverage[binEdges, 2], counts}];

enter image description here

Then as you say, BarChart is not so good:

    BarChart[dataPoints[[;; , 2]], ChartLabels -> dataPoints[[;; , 1]]]

enter image description here

However, ListStepPlot makes things very easy!

    ListStepPlot[dataPoints, "Center", Filling -> Axis]

enter image description here

And you can use all the normal plot styling functionality

    ListStepPlot[dataPoints, "Center", 
      PlotStyle -> RGBColor[0.982864`, 0.7431472`, 0.3262672`], 
      Filling -> Axis, 
      FillingStyle -> Opacity[1], 
      Frame -> True, 
      PlotRangePadding -> None, 
      GridLines -> Automatic]

Good Luck!

enter image description here

$\endgroup$
3
$\begingroup$

One way would be like the following. Assume that this is your list:

lst = Table[{i, i^(1/2)*Exp[-i]}, {i, 0, 4, 0.05}];

And here is you histogram-like plot:

 Show[Graphics[{Blue, 
     Rectangle[{#[[1]] - 0.025, 0}, {#[[1]] + 0.025, #[[2]]}]}, 
    AspectRatio -> 1, Axes -> True] & /@ lst]

looking as follows:

enter image description here

Have fun!

$\endgroup$

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.