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.

I'm trying to create a histogram using ListPlot using the Filling->Axis and FillingStyle -> {{LightGray, Thickness[.1]}} as options. However, when I use this the columns are actually higher than the value in the lists. Below is an example

   n = 6;
   p = 0.5;
   (*calculate probabilities *)
   list = Table[
     c = Binomial[n, k];
     d = p^k (1 - p)^(n - k);
     {k, c* d}, {k, 0, n}]


   (*FillingStyle Thickness results in additional height being added to \
   top of each column*)
   ListPlot[(list), 
    Joined -> False,
    Axes -> True,
    FrameTicks -> {Table[i, {i, 0 , n, 1}], 
      Table[i, {i, 0, 0.3, 0.05}]},
    FrameLabel -> {"Successes k", "Probability"},
    Frame -> {True, True, False, False},
    PlotStyle -> Automatic,
    Filling -> Axis,
    FillingStyle -> {{LightGray, Thickness[.1]}},
    ImageSize -> 6*72 ]

Does anyone have a suggestion for a solution for this problem while still using ListPlot?

Thanks,

Mike

share|improve this question

4 Answers 4

up vote 8 down vote accepted

This can be controlled by CapForm

ListPlot[(list), Joined -> False, 
 FrameTicks -> {Table[i, {i, 0, n, 1}], Table[i, {i, 0, 0.3, 0.05}]}, 
 FrameLabel -> {"Successes k", "Probability"}, 
 Frame -> {True, True, False, False}, PlotStyle -> Automatic, 
 Filling -> Bottom, 
 PlotRangePadding -> {Automatic, {None, Automatic}}, 
 FillingStyle -> {{LightGray, Thickness[.1], CapForm[None]}}, 
 ImageSize -> 6*72]

enter image description here

share|improve this answer
    
Never knew about CapForm, but it's exactly what I needed. Thanks! –  mikemtnbikes Jan 20 at 19:08
BarChart[Labeled[#2, #1] & @@@ list]

Mathematica graphics

share|improve this answer

As you figured out, the Thickness command applies to the width of the Filling line in both the horizontal and vertical directions. I don't see how to make the boxes wider without also increasing their maximum height.

Here is a quick and dirty way to get what you want:

ListPlot[Flatten[
  Table[{list[[n, 1]] + dx, list[[n, 2]]}, {n, 
    Length[list]}, {dx, -.2, .2, .001}], 1], Filling -> Axis, 
 FillingStyle -> Directive[LightGray, Thickness[.001]], 
 PlotStyle -> None, Epilog -> First@ListPlot[list]]

enter image description here

Edit: This is a better way to get what you are looking for.

    dx = 0.2; (*half width of the rectangles *)
Show[Table[
  Graphics[{LightGray, 
    Rectangle[{list[[n, 1]] - dx, 0}, {list[[n, 1]] + dx, 
      list[[n, 2]]}]}], {n, Length[list]}], ListPlot[(list)], 
 PlotRange -> {0, All}, 
 FrameTicks -> {Table[i, {i, 0, n, 1}], Table[i, {i, 0, 0.3, 0.05}]}, 
 FrameLabel -> {"Successes k", "Probability"}, 
 Frame -> {True, True, False, False}, AspectRatio -> .6, 
 ImageSize -> 6*72]

enter image description here

(Although, as belisarius pointed out, you can use BarChart to get what you want, but you'll need to fiddle around with it to get better looking results).

share|improve this answer

I think you need to make some compromise for creating histogram by using ListPlot...I would suggest...

PlotStyle ->PointSize[Large],
Filling -> Axis,
FillingStyle -> {{LightGray, {Thickness[.025]}}}

enter image description here

Or use barchart

share|improve this answer

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.