4
$\begingroup$

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

$\endgroup$

4 Answers 4

11
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Never knew about CapForm, but it's exactly what I needed. Thanks! $\endgroup$ Jan 20, 2014 at 19:08
2
$\begingroup$
BarChart[Labeled[#2, #1] & @@@ list]

Mathematica graphics

$\endgroup$
1
$\begingroup$

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).

$\endgroup$
0
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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