2
$\begingroup$

Let's consider the following simple data set

data = {{0,1.48},{1,0},{2.3,6.22},{3.1,0.01},{3.45,2.66},{5.34,0.11},
        {7.98,3.18},{8.12,4.11},{8.32,0},{9.65,3.56},{10,5.22}};

The list contains pairs $(x,y)$. I want to use this data for creating a histogram where the $x$ values are the horizontal values of the histogram, while the corresponding $y$ values indicate the height of the respective bar of the histogram. I also should be able to control the width of the bars, let's say to be equal to 0.001.

Any suggestions on how to create the desired histogram?

$\endgroup$
  • $\begingroup$ are the x values mid-points of bins or bin widths? $\endgroup$ – kglr Jun 18 at 9:09
  • $\begingroup$ @kglr Well, the x values are the central values of the bins. However, I need to be able to control the width of the bin, let's say to be equal to 0.001. $\endgroup$ – Vaggelis_Z Jun 18 at 9:14
  • $\begingroup$ related : Histogram based on pre-binned data $\endgroup$ – kglr Jun 18 at 9:33
  • $\begingroup$ @kglr ListPlot is not what I want. What I want is a real histogram where I can control the width of the boxes. $\endgroup$ – Vaggelis_Z Jun 18 at 9:41
  • 1
    $\begingroup$ You don't have a histogram. A histogram is constructed from "counts" from a sample of observations. There is no evidence that the $y$ values are related to counts in any way. Also, if the bins are already chosen, you certainly can't create smaller bin widths. Are the heights related to frequencies or means or what? $\endgroup$ – JimB Jun 18 at 15:24
3
$\begingroup$

Just convert your data to WeightedData with WeightedData @@ Transpose[data]:

Histogram[WeightedData @@ Transpose[data], 10]
$\endgroup$
  • $\begingroup$ What is the purpose of 10 inside the histogram commend? $\endgroup$ – Vaggelis_Z Jun 18 at 10:36
  • $\begingroup$ That just tells Histogram how many bins to use. You can specify the bins in a variety of ways. Automatic will leave Histogram to figure it out for you, but it doesn't always do a very good job. Specifying an integer like 10 tells it to use 10 bins. If you want to specify the bin width explicitly, you can use {width} (e.g., {1} for bins 1 unit wide). $\endgroup$ – Sjoerd Smit Jun 18 at 10:45
3
$\begingroup$

How about this?

width = 0.1;
{x, y} = Transpose@{{0, 1.48}, {1, 0}, {2.3, 6.22}, {3.1, 
     0.01}, {3.45, 2.66}, {5.34, 0.11}, {7.98, 3.18}, {8.12, 
     4.11}, {8.32, 0}, {9.65, 3.56}, {10, 5.22}};

Graphics[{Lighter@Blue, 
  Table[Rectangle[{x[[i]], 0}, {x[[i]] + width, y[[i]]}], {i, 
    Length@x}]}, Frame -> True]

enter image description here

Or

width = 0.1;
data = {{0, 1.48}, {1, 0}, {2.3, 6.22}, {3.1, 0.01}, {3.45, 
    2.66}, {5.34, 0.11}, {7.98, 3.18}, {8.12, 4.11}, {8.32, 0}, {9.65,
     3.56}, {10, 5.22}};
Graphics[{Lighter@Blue, 
  Rectangle[{#1, 0}, {#1 + width, #2}] & @@@ data}, Frame -> True]
$\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.