MHB How can I create a custom histogram with specific bin widths using Wolfram?

  • Thread starter Thread starter Jameson
  • Start date Start date
AI Thread Summary
The discussion centers on creating a histogram with custom bin widths using Wolfram Alpha and Mathematica. The user seeks to graph a specific data set over defined ranges, but encounters limitations with Wolfram Alpha's online capabilities for customizing bin widths. A solution is provided using Mathematica's Histogram function, specifying bin widths with the command Histogram[data, {.5}]. The user also expresses frustration with attempting to create a boxplot, initially using incorrect syntax. The correct command, BoxWhiskerChart, is suggested, along with additional statistical information that Mathematica provides. The conversation shifts to alternatives for obtaining Mathematica, with suggestions to consider free options like Python or check for school licenses. Additionally, a method for creating histograms in LaTeX using PGFPlots is shared, demonstrating another approach to visualizing the data.
Jameson
Insights Author
Gold Member
MHB
Messages
4,533
Reaction score
13
I'm trying to create a simple histogram with custom bin widths. I want the data to be graphed over the ranges:

$[(0,.5],(.5,1],(1,1.5],(1.5,2],(2,2.5],(2.5,3],(3,3.5])$

Here is my data set:

{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}

If I enter this into Wolfram Alpha:

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

I get a graph but I can't seem to customize the bin width. Any ideas?
 
Physics news on Phys.org
Re: Histogram on Wolfram

Jameson said:
I'm trying to create a simple histogram with custom bin widths. I want the data to be graphed over the ranges:

$[(0,.5],(.5,1],(1,1.5],(1.5,2],(2,2.5],(2.5,3],(3,3.5])$

Here is my data set:

{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}

If I enter this into Wolfram Alpha:

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

I get a graph but I can't seem to customize the bin width. Any ideas?

The general syntax for Histogram is Histogram[data,bspec,hspec] (but hspec isn't needed here). So the bspec you want to specify is {.5} (since you seem to be splitting the data into intervals that are .5 units long); thus, the command

Code:
Histogram[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725},{.5}]

gives me the following graph in Mathematica:


Is this what you're after?
 
Re: Histogram on Wolfram

Yep that's it and exactly what I typed into Wolfram Alpha, but apparently it doesn't work on the online version. :(
 
Now I'm trying to do a boxplot (this is ridiculously simple I know - it's a class I have to take) and according to the online guide this code should work:

Code:
BoxWhiskerPlot[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

but alas, nothing. I found another site to make the box plot but I wish this could be done online at W|A.
 
Jameson said:
Now I'm trying to do a boxplot (this is ridiculously simple I know - it's a class I have to take) and according to the online guide this code should work:

Code:
BoxWhiskerPlot[{0.45,0.45,0.473,0.507,0.457,0.452,0.453,1.215,1.256,1.145,1.085,1.066,1.111,1.364,1.254,1.396,1.575,1.617,1.733,2.753,3.186,3.227,3.469,1.911,2.588,2.635,2.725}]

but alas, nothing. I found another site to make the box plot but I wish this could be done online at W|A.

It should be BoxWhiskerChart. Plugging that code (after making that correction) into Mathematica gives me the following:


There was also some additional information you get when you hover over the output in Mathematica:

Max: 3.469
75%: 2.588
Median: 1.256
25%: 0.507
Min: 0.45
 
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!
 
Jameson said:
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!

Yea, no problem. Since you're a student, you can get Mathematica for about $140 (if you get the standard edition [which is the one I have]; its cheaper if you want to have it for a year or even a semester [see here for more info]). If you get the standard edition, it's yours to keep even after you're done with school.
 
Jameson said:
I just need to get a copy of Mathematica on my laptop. Wolfram Alpha doesn't have the full functionality Mathematica does and this code still doesn't work there. Thanks for the help!

Why pay $140 when Python is FREE. Also, every school I have been to gave me a site license. You should look into that before paying for it to. However, still go with Python.

If you are going to put this graphics in a \(\LaTeX\) document, you could always create it in PGFplots. Here is your code with 7 bins. You would have to look up how to specify bin width but that shouldn't be too hard.

PGFPlots documentation: CTAN: Package pgfplots



Latex Code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar interval,
xticklabel =
{$(\pgfmathprintnumber\tick, %
\pgfmathprintnumber\nexttick]$},
font = \scriptsize,
width = 15cm
]
\addplot+[hist = {bins = 7}]
table[row sep = \\, y index = 0]{
data\\
0.45\\ 0.45\\ 0.473\\ 0.507\\ 0.457\\ 0.452\\ 0.453\\ 1.215\\ 1.256\\
1.145\\ 1.085\\ 1.066\\ 1.111\\ 1.364\\ 1.254\\ 1.396\\ 1.575\\ 1.617\\
1.733\\ 2.753\\ 3.186\\ 3.227\\ 3.469\\ 1.911\\ 2.588\\ 2 .635\\ 2.725\\
};
\end{axis}
\end{tikzpicture}
\end{document}
 
Last edited:

Similar threads

Back
Top